Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scala implicit method with multiple arguments

Tags:

scala

implicit

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?

like image 231
xiefei Avatar asked Jun 07 '12 15:06

xiefei


1 Answers

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
like image 88
drexin Avatar answered Sep 28 '22 08:09

drexin