9

I thought the following would be the most concise and correct form to collect elements of a collection which satisfy a given type:

def typeOnly[A](seq: Seq[Any])(implicit tag: reflect.ClassTag[A]): Seq[A] = 
  seq.collect {
    case tag(t) => t
  }

But this only works for AnyRef types, not primitives:

typeOnly[String](List(1, 2.3, "foo"))  // ok. List(foo)
typeOnly[Double](List(1, 2.3, "foo"))  // fail. List()

Obviously the direct form works:

List(1, 2.3, "foo") collect { case d: Double => d }  // ok. List(2.3)

So there must be a (simple!) way to fix the above method.

0__
  • 66,707
  • 21
  • 171
  • 266

1 Answers1

20

It's boxed in the example, right?

scala> typeOnly[java.lang.Double](vs)
res1: Seq[Double] = List(2.3)

Update: The oracle was suitably cryptic: "boxing is supposed to be invisible, plus or minus". I don't know if this case is plus or minus.

My sense is that it's a bug, because otherwise it's all an empty charade.

More Delphic demurring: "I don't know what the given example is expected to do." Notice that it is not specified, expected by whom.

This is a useful exercise in asking who knows about boxedness, and what are the boxes? It's as though the compiler were a magician working hard to conceal a wire which keeps a playing card suspended in midair, even though everyone watching already knows there has to be a wire.

scala> def f[A](s: Seq[Any])(implicit t: ClassTag[A]) = s collect {
     | case v if t.runtimeClass.isPrimitive &&
     |   ScalaRunTime.isAnyVal(v) &&
     |   v.getClass.getField("TYPE").get(null) == t.runtimeClass =>
     |   v.asInstanceOf[A]
     | case t(x) => x
     | }
f: [A](s: Seq[Any])(implicit t: scala.reflect.ClassTag[A])Seq[A]

scala> f[Double](List(1,'a',(),"hi",2.3,4,3.14,(),'b'))
res45: Seq[Double] = List(2.3, 3.14)

ScalaRunTime is not supported API -- the call to isAnyVal is just a match on the types; one could also just check that the "TYPE" field exists or

Try(v.getClass.getField("TYPE").get(null)).map(_ == t.runtimeClass).getOrElse(false)

But to get back to a nice one-liner, you can roll your own ClassTag to handle the specially-cased extractions.

Version for 2.11. This may not be bleeding edge, but it's the recently cauterized edge.

object Test extends App {

  implicit class Printable(val s: Any) extends AnyVal {
    def print = Console println s.toString
  }

  import scala.reflect.{ ClassTag, classTag }
  import scala.runtime.ScalaRunTime

  case class Foo(s: String)

  val vs = List(1,'a',(),"hi",2.3,4,Foo("big"),3.14,Foo("small"),(),null,'b',null)

  class MyTag[A](val t: ClassTag[A]) extends ClassTag[A] {
    override def runtimeClass = t.runtimeClass
    /*
    override def unapply(x: Any): Option[A] = (
      if (t.runtimeClass.isPrimitive && (ScalaRunTime isAnyVal x) &&
          x.getClass.getField("TYPE").get(null) == t.runtimeClass)
        Some(x.asInstanceOf[A])
      else super.unapply(x)
    )
    */
    override def unapply(x: Any): Option[A] = (
      if (t.runtimeClass.isPrimitive) {
        val ok = x match {
          case _: java.lang.Integer   => runtimeClass == java.lang.Integer.TYPE
          //case _: java.lang.Double    => runtimeClass == java.lang.Double.TYPE
          case _: java.lang.Double    => t == ClassTag.Double  // equivalent
          case _: java.lang.Long      => runtimeClass == java.lang.Long.TYPE
          case _: java.lang.Character => runtimeClass == java.lang.Character.TYPE
          case _: java.lang.Float     => runtimeClass == java.lang.Float.TYPE
          case _: java.lang.Byte      => runtimeClass == java.lang.Byte.TYPE
          case _: java.lang.Short     => runtimeClass == java.lang.Short.TYPE
          case _: java.lang.Boolean   => runtimeClass == java.lang.Boolean.TYPE
          case _: Unit                => runtimeClass == java.lang.Void.TYPE
          case _ => false // super.unapply(x).isDefined
        }
        if (ok) Some(x.asInstanceOf[A]) else None
      } else if (x == null) {  // let them collect nulls, for example
        if (t == ClassTag.Null) Some(null.asInstanceOf[A]) else None
      } else super.unapply(x)
    )
  }

  implicit def mytag[A](implicit t: ClassTag[A]): MyTag[A] = new MyTag(t)

  // the one-liner
  def g[A](s: Seq[Any])(implicit t: ClassTag[A]) = s collect { case t(x) => x }

  // this version loses the "null extraction", if that's a legitimate concept
  //def g[A](s: Seq[Any])(implicit t: ClassTag[A]) = s collect { case x: A => x }

  g[Double](vs).print
  g[Int](vs).print
  g[Unit](vs).print
  g[String](vs).print
  g[Foo](vs).print
  g[Null](vs).print
}

For 2.10.x, an extra line of boilerplate because implicit resolution is -- well, we won't say it's broken, we'll just say it doesn't work.

// simplified version for 2.10.x   
object Test extends App {
  implicit class Printable(val s: Any) extends AnyVal {
    def print = Console println s.toString
  }
  case class Foo(s: String)

  val vs = List(1,'a',(),"hi",2.3,4,Foo("big"),3.14,Foo("small"),(),null,'b',null)

  import scala.reflect.{ ClassTag, classTag }
  import scala.runtime.ScalaRunTime

  // is a ClassTag for implicit use in case x: A
  class MyTag[A](val t: ClassTag[A]) extends ClassTag[A] {
    override def runtimeClass = t.runtimeClass
    override def unapply(x: Any): Option[A] = (
      if (t.runtimeClass.isPrimitive && (ScalaRunTime isAnyVal x) &&
          (x.getClass getField "TYPE" get null) == t.runtimeClass)
        Some(x.asInstanceOf[A])
      else t unapply x
    )
  }

  // point of the exercise in implicits is the type pattern.
  // there is no need to neutralize the incoming implicit by shadowing.
  def g[A](s: Seq[Any])(implicit t: ClassTag[A]) = {
    implicit val u = new MyTag(t)  // preferred as more specific
    s collect { case x: A => x }
  }

  s"Doubles? ${g[Double](vs)}".print
  s"Ints?    ${g[Int](vs)}".print
  s"Units?   ${g[Unit](vs)}".print
  s"Strings? ${g[String](vs)}".print
  s"Foos?    ${g[Foo](vs)}".print
}

Promoting a comment:

@WilfredSpringer Someone heard you. SI-6967

som-snytt
  • 39,429
  • 2
  • 47
  • 129
  • 1
    Yes, but I would like to cover this case automatically instead of asking to use the boxed type as parameter. – 0__ May 30 '13 at 00:47
  • Unfortunately I don't think there is a nice solution to this problem. You can see some of the issues you'll face [here](http://stackoverflow.com/questions/13233728/scala-2-10-typetag-usage/13234888#13234888). You could probably use type tags and an instance mirrors to do what you want, but it certainly wouldn't be elegant. – Matt Roberts Jun 02 '13 at 20:45
  • 1
    the `.getClass.getField("TYPE").get(null)` is clever; I've been looking for a simple method to match a boxed class/type/symbol/... to its primitive one without the big pattern matching, but no luck so far. – gourlaysama Jun 03 '13 at 22:06
  • if you specify `MyTag` for the implicit param, you get a diverging implicit because `implicit def mytag` can provide itself its own parameter (since `MyTag` extends `ClassTag`). You could keep the `MyTag` implicit parameter and not make `MyTag` extend `ClassTag` but just provide `unapply` (which delegates to `t.runtimeClass`). *edit*: or you can just leave it as is, but it would (silently) fail if no `MyTag` is in scope, using a normal `ClassTag` instead... – gourlaysama Jun 03 '13 at 22:29
  • actually, the code with `g(...)(implicit t: ClassTag[A]) = ...` fails for me with 2.10.1. The normal `ClassTag` is picked up, not `MyTag`. – gourlaysama Jun 03 '13 at 22:39
  • @gourlaysama 2.10 version posted. Thanks for trying it. I don't hack with 2.10 because the repl lacks :javap under 1.7. – som-snytt Jun 07 '13 at 08:38
  • 1
    There's no need for a custom ClassTag. What I usually do is to extract the Class[_] and use a dictionary mapping back and forth between Class instances for `int` and `java.lang.Int`. Then you can use `ClassTag.apply` to rebuild a new tag for pattern-matcher consumption. – Blaisorblade Jun 10 '13 at 13:16
  • 1
    Here's a code snippet doing the conversion for `Class` instances: https://github.com/ps-mr/LinqOnSteroids/blob/master/src/main/scala/ivm/expressiontree/Util.scala#L7 (which in a perfect world would be in the standard library), and an implementation of `instanceOf` based on that: https://github.com/ps-mr/LinqOnSteroids/blob/master/src/main/scala/ivm/expressiontree/Util.scala#L83 This can easily be adapted. – Blaisorblade Jun 10 '13 at 13:22
  • Bonus of my solution: using internals of the scala runtime is not needed. – Blaisorblade Jun 10 '13 at 13:28
  • 1
    @Blaisorblade thx for the linx. Agreed. In a perfect world, tho', the OP code would just work out of the box. (pun alert) – som-snytt Jun 10 '13 at 15:05
  • I just got your pun alert, I thought the joke was just "perfect world" in reference to Scala. How late am I getting this! My, my, oh my! But the pun is actually nice. – Blaisorblade Jun 10 '13 at 20:33
  • 1
    Awesome. Finally solved the issue I had, causing me to wade through Manifests, ClassTags, TypeTags, Mirrors for hours. I have to say I would have rather this fixed in Scala than a whole bunch of things that are getting added and I don't really care about. – Wilfred Springer Apr 02 '14 at 10:58