render
方法意味着从Java使用,而apply
意味着从Scala使用。呈现代表apply
,他们将具有完全相同的签名除非有多个参数列表(来自柯里格或implicits)。
比方说,我已经从播放斯卡拉激活模板index.html.scala
,修改,添加一个隐含的Int
参数:
@(message: String)(implicit i: Int)
它会被编译到target/scala-2.11/twirl/main/views/html/index.template.scala
。以下是相关部分:
def apply(message: String)(implicit i: Int): play.twirl.api.HtmlFormat.Appendable = ...
def render(message: String, i: Int): play.twirl.api.HtmlFormat.Appendable =
apply(message)(i)
render
中的参数被压缩到一个列表中。既然你不能使用来自Java(或多个参数列表)的implicits,他们需要显式地传递到一个列表中。
如果我删除了隐式的,它们是相同的:
def apply(message: String): play.twirl.api.HtmlFormat.Appendable = ...
def render(message:String): play.twirl.api.HtmlFormat.Appendable = apply(message)
尼斯的答案!这就解释了为什么'render'不能编译,尽管我在范围内有适当的含义。 –