PlayFramework scala template generic types cause error: ']' expected but eof found. And error: identifier expected but $XMLSTART$< found

PlayFramework scala template generic types cause error: ']' expected but eof found. And error: identifier expected but $XMLSTART$< found

我正在尝试向 scala templates 添加一些来自 java[=46= 的通用变量], 但遗憾的是到目前为止运气不佳。

不是 Scala 专家。我只是日常Java开发者.

我选择了 Play,因为它在 Java 世界中似乎是一个简单的框架。 现在我在使用这个 Scala 模板时遇到了问题。

我在 scala tutorial 中创建,我必须添加 plus 字符来定义扩展。

这是我的代码:

实体class:

public class Insurance<T> {
    // some code
}

列表声明(这里没有声明类型参数):

List<Entity> entities = new LinkedList<Entity>();

向视图模板渲染方法传递参数:

return ok(views.html.Index.render(entities));

在 Scala 视图层上:

@(entities: List[models.Entity[+T]])

但是我收到一个错误:

[error] /home/app/views/Index.scala.html:0: identifier expected but $XMLSTART$< found.
[error] /home/app/views/Index.scala.html:150: ']' expected but eof found.

当我删除泛型时它会起作用。

请帮忙。

模板实际上就像一个方法,就像一个方法参数列表(entities: List[models.Entity[+T]])并不比public Something myTemplate(entities: List[models.Entity[+T]])更有效,只是没有地方放方法调用的类型参数,因此您必须提供具体类型而不是 T

此外,在 Scala 中,类型参数的变体(+)并未在调用点指定,而是在声明中指定,因此即使您可以定义类型也不会有效方法签名中的参数。

你可以做的就是说它可以是任何东西,使用 models.Entity[Any] 或者可能只是你不关心使用 models.Entity[_](基本上就像 models.Entity<?> Java).

作为旁注,您似乎应该多考虑一两次,因为如果您可以使用完全未绑定的类型参数 T 那么这意味着您什么都不知道关于它在模板内部,如果你对此一无所知,你会在模板中用它做什么?

希望对您有所帮助!