scala implicit method with multiple arguments

2019-06-19 05:12发布

In a comment for SIP-13 Martin Odersky implied that it is possible to create an implicit method with multiple arguments. According to my experiences, implicit methods always have exactly one argument and I cannot imagine how an implicit method with multiple arguments can be used. Can someone give some use case and explanation?

1条回答
干净又极端
2楼-- · 2019-06-19 05:50

For example if you need an implicit parameter of a function type:

implicit def foo(x: Int, y: Int) = y * x

def bar(x: Int, y: Int)(implicit f: (Int, Int) => Int) = f(x,y)

scala> bar(3,4)
res3: Int = 12
查看更多
登录 后发表回答