This question is a follow-on to this one: Invoking Case Class Constructor
The answer I selected works great--except for a few cases involving value classes. In these cases I try code like this:
// cc.ctor is an reflected MethodMirror as described in linked question.
println(s"::: Poof: ${cc.name} with "+args.toList)
println("--- Ctor : "+cc.ctor)
println("--- Args : "+args.toList.map(_.getClass.getName))
println("--- Tried: "+scala.util.Try( cc.ctor.apply(args:_*) ))
The objects involved are below. Wrapped is the object being constructed.
case class Wrapped( hey:Wrapper, you:String )
class Wrapper(val underlying: String) extends AnyVal
Here's the output I get:
::: Poof: co.blocke.scalajack.test.v4.Wrapped with List(co.blocke.scalajack.test.v4.Wrapper@18cc6, greg)
--- Ctor : constructor mirror for def <init>(hey: co.blocke.scalajack.test.v4.Wrapper,you: String): co.blocke.scalajack.test.v4.Wrapped (bound to null)
--- Args : List(co.blocke.scalajack.test.v4.Wrapper, java.lang.String)
--- Tried: Success(Wrapped(co.blocke.scalajack.test.v4.Wrapper@18cc6,null))
Sooo close! You can see in the last line the Wrapped object, including its member value class, is "successfully" created, but where's my String? The constructor expects one. I passed one in in the args, but I get a big, fat null in my object. What's going on?
Aucun commentaire:
Enregistrer un commentaire