I have a case class with a companion object. I have implicit conversion method inside the companion object.
case class Foo(p:T)
object Foo {
implicit def Foo2Bar(foo: Foo): Bar = new Bar(doSmth(foo.p))
}
I have a method with a param of type Object
. I want to pass an instance of Bar
there.
Unfortunatelly, the following code doesn't do the conversion but throws ClassCastException
:
import Foo._
...
val foo = createFoo()
bazz(foo.asInstanceOf[Bar])
At the same time, the next (more verbose) code do the job:
import Foo._
...
val foo = createFoo()
val bar: Bar = foo
bazz(bar)
Any ideas why the former code doesn't work?