Search code examples
scalaimplicit-conversionimplicit

Scala: implicitly to implicit class


Given:

implicit class Foo(val i: Int) {
   def addValue(v: Int): Int = i + v
}

is it possible apply to it any implicitly? I get an error here:

<console>:14: error: could not find implicit value for parameter e: Foo
       implicitly[Foo]

Solution

  • An implicit class Foo(val i: Int) means that there is an implicit conversion from Int to Foo. So implicitly[Int => Foo] should work.

    Think about it like this: if you could summon a Foo with implicitly[Foo], which Foo would you expect to get? A Foo(0)? A Foo(1)? A Foo(2)?