dimanche 13 août 2017

Instance case class from companion object

I have the following:

trait C {}

object O {
    case class Foo(bar: String) extends C
}

And I would like to instance Foo from a String. Until the moment I have achieved instanciate Foo, but I cannot cast to C. I am using:

val ob = runtimeMirror.staticModule("O.Foo")
val foo = runtimeMirror.reflectModule(ob).instance

Now foo is an instance of O.Foo, but it cannot be cast to C.

val c = foo.asInstanceOf[C] 

This very last line returns:

O$foo$ cannot be cast to C





Aucun commentaire:

Enregistrer un commentaire