Scala: Passing one implicit parameter implicitly and the other explicitly. Is it possible?

jhegedus picture jhegedus · Mar 21, 2014 · Viewed 15k times · Source

Let's consider the function:

def foo(implicit a:Int, b:String) = println(a,b).

Now, let us assume that there is an implicit String and Int (implicit val i1=1) in scope but we want to pass an other, not implicit Int (val i2=2) explicitly to foo .

How can we do that ? Is it possible? Thanks for reading.

Answer

Peter Schmitz picture Peter Schmitz · Mar 21, 2014

All I can add is:

def foo(implicit a: Int, b: String) = println(a, b)
implicit val i1 = 1
implicit val s = ""
val i2 = 2
foo(i2, implicitly[String])