Play framework (2.3.5) - Template accessing an inn

2019-07-21 04:34发布

I have a class defined like so (pseudo code):

package com.some.namespace

public class SomeClass {

    protected SomeClass() {}

    // stuff...

    public class SomeInnerClass {

        public SomeInnerClass() {}

        // more stuff (accesses instance variables from SomeClass)...

    }

}

Then in my template I use the reusable block defines like so:

@doSomething(val: com.some.namespace.SomeClass.SomeInnerClass) = {

    // even more stuff...

}

But I get the error:

type SomeInnerClass is not a member of object com.some.namespace.SomeClass

Am I not able to access inner classes within the templates or is this supposed to work? (if it is supposed to work I might need to post my actual code)

1条回答
劫难
2楼-- · 2019-07-21 05:14

It should work, but because SomeInnerClass is a dependent type, i.e. dependent on your singleton outer class instance, you need to write it like:

@doSomething(value: com.some.namespace.SomeClass#SomeInnerClass) = {
    // even more stuff...
}

The SomeClass#SomeInnerClass syntax in Scala means, vaguely, a SomeInnerClass from any instance of SomeClass. See this answer for more detail.

If the inner class was static, however, your current SomeClass.SomeInnerClass syntax would be the way to go.

查看更多
登录 后发表回答