具有多个隐式参数的函数文字

lam*_*das 9 syntax arguments scala function syntax-error

如何在Scala中使用多个隐式参数定义函数文字?我试过这种方式:

def create = authAction { (implicit request, user) ? // Syntax error
  Ok(html.user.create(registrationForm))
}
Run Code Online (Sandbox Code Playgroud)

但它会抛出编译错误.

lam*_*das 17

如前面的答案中所述,您只能为函数文字定义单个隐式参数,但有解决方法.

您可以将函数文字编写为多个参数列表,而不是每个参数列表,而不是多个隐式参数.然后可以将每个参数标记为隐式.重写原始片段:

def create = authAction { implicit request ? implicit user ?
  Ok(html.user.create(registrationForm))
}
Run Code Online (Sandbox Code Playgroud)

你可以从authActionas 调用它f(request)(user).

implicit 关键字复制很烦人,但至少它有效.


pag*_*_5b 6

根据我对语言规范的理解,从版本2.9.2开始,您只能为匿名函数定义一个隐式参数.

例如

val autoappend = {implicit text:String => text ++ text}
Run Code Online (Sandbox Code Playgroud)