[英]How do you create scala anonymous function with multiple implicit parameters
I have scala functions called "run1" and "run2" which accept 3 parameters. 我有称为“run1”和“run2”的scala函数,它接受3个参数。 When I apply them I want to provide an anonymous function with implicit parameters.
当我应用它们时,我想提供一个带隐式参数的匿名函数。 It doesn't work in both cases in example codes below.
它在下面的示例代码中不适用于这两种情况。 I want to know if
我想知道是否
object Main extends App {
type fType = (Object, String, Long) => Object
def run1( f: fType ) {
f( new Object, "Second Param", 3)
}
run1 { implicit (p1, p2, p3) => // fails
println(p1)
println(p2)
println(p3)
new Object()
}
def run2( f: fType ) {
val fC = f.curried
fC(new Object)("Second Param")(3)
}
run2 { implicit p1 => implicit p2 => implicit p3 => // fails
println(p1)
println(p2)
println(p3)
new Object()
}
}
You're currying the function inside run2
so run2
still needs a non-curried function. 你正在使用
run2
的函数,因此run2
仍然需要一个非run2
函数。 See the code below for a version that works: 有关适用的版本,请参阅下面的代码:
object Main extends App {
type fType = (Object, String, Long) => Object
type fType2 = Object => String => Long => Object //curried
def run1( f: fType ) {
f( new Object, "Second Param", 3)
}
// Won't work, language spec doesn't allow it
run1 { implicit (p1, p2, p3) =>
println(p1)
println(p2)
println(p3)
new Object()
}
def run2( f: fType2 ) {
f(new Object)("Second Param")(3)
}
run2 { implicit p1 => implicit p2 => implicit p3 =>
println(p1)
println(p2)
println(p3)
new Object()
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.