Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassTag based pattern matching fails for primitives

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.

like image 845
0__ Avatar asked May 30 '13 00:05

0__


1 Answers

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

like image 71
som-snytt Avatar answered Oct 07 '22 15:10

som-snytt