Scala 隐性:如何让隐性价值生效?

Scala 隐性:如何让隐性价值生效?,scala,Scala,如何让隐式值p2生效 def test(f: =>Int=>Int=>Int)={ f(1) //it won't use the implicit p2. } val p1:Int=1 implicit val p2:Int=2 //here define a implicit value test{a1=>implicit a2=>a1+a2} // here define a implicit function, but how to let i

如何让隐式值p2生效

def test(f: =>Int=>Int=>Int)={
   f(1)  //it won't use the implicit p2.
}
val p1:Int=1
implicit val p2:Int=2   //here define a implicit value
test{a1=>implicit a2=>a1+a2}  // here define a implicit function, but how to let it use p2 which defined implicitly?
我试图伪造一个类似于下面调用isAuthenticated的示例,它的信号为
isAuthenticated{username=>隐式请求=>Ok(“Hello”+username)}

test
不(也不能)知道
f
的第二个参数是隐式的。你可以写

def test(f: =>Int=>Int=>Int)={
   f(1)(implicitly[Int])
}
这要求隐式
Int
在范围内(在您的情况下,
p2

此外,似乎没有理由让参数
test
isAuthenticated
(即
=>Int=>Int=>Int
而不是
Int=>Int
),这只会增加开销

def test(f: =>Int=>Int=>Int)={
   f(1)(implicitly[Int])
}