我想传递一个带有签名的函数
def insert(o: Role)(implicit s: Session): UUID
进入inserter
具有签名的功能(如)
def insertRows[T](instanceList: List[T], inserter: T => UUID): Unit
我如何指定inserter
具有隐式Session
?
首先,您应该了解如何从方法中创建一个需要隐式的函数:
scala> def a(a: Int)(implicit b: Int) = a
a: (a: Int)(implicit b: Int)Int
scala> a _
<console>:9: error: could not find implicit value for parameter b: Int
a _
^
//I assume you can't specify implicit before `a _`, otherwise you have your answer anyway :)
scala> a(_: Int)(_: Int)
res18: (Int, Int) => Int = <function2>
然后,清楚地知道要传递什么:
scala> def f(f: (Int, Int) => Int) =0
f: (f: (Int, Int) => Int)Int
scala> f(a(_: Int)(_: Int))
res16: Int = 0
甚至:
scala> f(a(_)(_))
res25: Int = 0
对于任何其他咖喱函数,它都有效。我希望有一天,scala会变得足够聪明,以支持相同的隐式方式。
PS在您的特定情况下:
def insertRows[T](instanceList: List[T], inserter: (T, Session) => UUID): Unit
insertRows[Role](list, insert(_)(_))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句