Future.mapTo[S] casts a value of type S to a value at type T with Class.cast, but needs to take boxing into account. The code for that is generally useful and IMHO should become part of the reflection API.
From Future:
private[concurrent] val toBoxed = Map[Class[_], Class[_]](
classOf[Boolean] -> classOf[java.lang.Boolean],
classOf[Byte] -> classOf[java.lang.Byte],
classOf[Char] -> classOf[java.lang.Character],
classOf[Short] -> classOf[java.lang.Short],
classOf[Int] -> classOf[java.lang.Integer],
classOf[Long] -> classOf[java.lang.Long],
classOf[Float] -> classOf[java.lang.Float],
classOf[Double] -> classOf[java.lang.Double],
classOf[Unit] -> classOf[scala.runtime.BoxedUnit]
)
def boxedType(c: Class[_]): Class[_] = {
if (c.isPrimitive) Future.toBoxed(c) else c
}
def mapTo[S](implicit tag: ClassTag[S]) = {
boxedType(tag.runtimeClass).cast(t).asInstanceOf[S]
}
I believe this is generally useful because I've also written some very similar code, although apparently I didn't get it as correct or elegant (why is Unit not boxed to java.lang.Void?):
Best regards
Paolo