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'不能編譯,儘管我在範圍內有適當的含義。 –