Question

I've always thought that Scala implicit conversions could only take one argument, and if you needed something like conversion from multiple arguments, you would use a tuple. However, I was surprised to find that Scala 2.10.3 actually accepts this (silly) definition without problem:

scala> implicit def foo(i: Int, j: Int): Int = i
foo: (i: Int, j: Int)Int

Is there any way or reason to use such a multi-argument implicit?

Was it helpful?

Solution

Not all implicit functions are conversions!

Any implicit function with a single argument could be used as a conversion, but that's not the only use-case for implicits.

Functions are just another type and, as such, you can specify an implicit argument as in (implicit fn: (Int, Int) => Int)

I wouldn't do this though, anything you intend to pass as an implicit should have a far more specific type that better self-documents its intended purpose.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top