Scala: implicitly converted type as implicit param of a method

Viewed 499

I have a method that expects some implicit param of type String.

I have an object of class MyClass which I want to pass to that method implicitly. First, I'm making implicit conversion from MyClass to String.

If I pass object of MyClass to the method which expects String explicitly, it works fine.

But passing it implicitly causes compilation error:

could not find implicit value for parameter str: String someMethod()

Here is the code:

case class MyClass(str: String)
object MyClass {
  implicit def myClassToString(myClass: MyClass): String = myClass.str
}

object Application extends App {
  def someMethod()(implicit str: String) = println(str)
  implicit val myClass = MyClass("Hello")
  someMethod()(myClass) <-- WORKS
  someMethod() <-- ERROR
}

Is it possible to pass implicitly converted variable implicitly?

P.S.: I cannot change someMethod

1 Answers
Related