Neilos
Neilos

Reputation: 2746

Scala - unbound wildcard exception (Play Framework 2.3 Template)

I am using Play Framework 2.3 I am using the scala template engine to create my views and Java elsewhere.

My model extends an abstract parameterised object like so... (pseudo code)

Abstract object:

public abstract class MyObject<T> {

    // various bits

    public class MyInnerObject {

        // more stuff

    }

}

Model object (singleton)

public class SomeModel extends MyObject<SomeBean> {

    public static SomeModel getInstance() {
        if (instance == null)
            instance = new SomeModel();
        return instance;
    }

    // more bits

}

I then pass the model to the view from another view helper:

@MyHelper(SomeModel.getInstance())

MyHelper scala view template:

@*******************************************
 * My helper
 *******************************************@

@(myObj: some.namespace.MyObject[_])

@import some.namespace.MyObject

@doSomething(myInnerObj: MyObject[_]#MyInnerObject) = {
    @* do some stuff *@
}

    @for(myInnerObj <- myObj.getInnerObjects()) {
        @doSomething(myInnerObj)
    }

However I get an error on the line @doSomething(myInnerObj: MyObject[_]#MyInnerObject) stating

unbound wildcard exception

I am not sure the correct Scala syntax to avoid this error I had naively assumed that I could use the _ to specify arbitrary tyope but it won't let me do this.

What is the correct syntax?

UPDATE 1

Changing the method definition to:

@doSomething[T](myInnerObj: MyObject[T]#MyInnerObject)

gives further errors:

no type parameters for method doSomething: (myInnerObj:[T]#MyInnerObject)play.twirl.api.HtmlFormat.Appendable exist so that it can be applied to arguments (myObj.MyInnerObject)
--- because ---
argument expression's type is not compatible with formal parameter type;
found : myObj.MyInnerObject
required: MyObject[?T]#MyInnerObject

It would seem that the Twirl templating engine does not support this syntax currently, although I'm not 100% sure.

Upvotes: 0

Views: 209

Answers (1)

Neilos
Neilos

Reputation: 2746

I can solve the problem by removing the doSomething method completely...

@*******************************************
 * My helper
 *******************************************@

@(myObj: some.namespace.MyObject[_])

@import some.namespace.MyObject

    @for(myInnerObj <- myObj.getInnerObjects()) {
        <div>@myInnerObj.getSomeProperty()</div>
    }

But I am bout 10% happy with the solution... It works at least but it feels very restricting that I cannot delegate to methods to help keep my code maintainable. By the look of the comments the problem seems to be a limitation in Twirl, not allowing type arguments for functions in views.

Note: I have accepted this answer as it removes the original problem of the exception however this is only because the solution I want doesn't exist... yet.

Upvotes: 0

Related Questions