Exposing Future.toBoxed and related machinery

31 views
Skip to first unread message

Paolo G. Giarrusso

unread,
Oct 19, 2012, 4:32:53 PM10/19/12
to scala-i...@googlegroups.com
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

Paul Phillips

unread,
Oct 19, 2012, 5:14:47 PM10/19/12
to scala-i...@googlegroups.com


On Fri, Oct 19, 2012 at 1:32 PM, Paolo G. Giarrusso <p.gia...@gmail.com> wrote:
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?):

Have you an instance of java.lang.Void squirreled away somewhere?

√iktor Ҡlang

unread,
Oct 19, 2012, 5:18:40 PM10/19/12
to scala-i...@googlegroups.com

val v = new Void // Put the lime in the coconut and shake it all up

Paolo Giarrusso

unread,
Oct 19, 2012, 5:51:25 PM10/19/12
to scala-i...@googlegroups.com
Still, what about the main idea?
I see your point. OK, I'll check my side remarks next time, and have
more unit tests for my code.

But what about the main idea of exposing the code from Future.toBoxed
code in the Scala (reflection) library?
--
Paolo G. Giarrusso - Ph.D. Student, Philipps-University Marburg
http://www.informatik.uni-marburg.de/~pgiarrusso/

Paul Phillips

unread,
Oct 19, 2012, 5:57:17 PM10/19/12
to scala-i...@googlegroups.com


On Fri, Oct 19, 2012 at 2:51 PM, Paolo Giarrusso <pgiar...@mathematik.uni-marburg.de> wrote:
But what about the main idea of exposing the code from Future.toBoxed
code in the Scala (reflection) library?

My only reservation is that it doesn't go far enough, but yes, some convenient way to jump between these things and lots of variations on them would be wonderful.  I've written that method so many times I no longer see the method - it's just blonde, brunette.

√iktor Ҡlang

unread,
Oct 19, 2012, 6:31:16 PM10/19/12
to scala-i...@googlegroups.com

BoxesRuntime or something like that?

Cheers,
V

Reply all
Reply to author
Forward
0 new messages