基于 ClassTag 的模式匹配对于基元失败 [英] ClassTag based pattern matching fails for primitives

查看:31
本文介绍了基于 ClassTag 的模式匹配对于基元失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为以下是收集满足给定类型的集合元素的最简洁和正确的形式:

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
  }

但这仅适用于 AnyRef 类型,不适用于原语:

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()

显然直接形式有效:

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.

推荐答案

它在示例中是盒装的,对吗?

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.

更多的 Delphic 反驳:我不知道给定的示例会做什么."请注意,它没有指定,由谁期望.

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 不受支持的 API -- 对 isAnyVal 的调用只是类型上的匹配;也可以只检查TYPE"字段是否存在或

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)

但要回到一个不错的单行代码,您可以使用自己的 ClassTag 来处理特殊情况下的提取.

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

2.11 版本.这可能不是最前沿的,但它是最近烧灼的边缘.

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
}

对于 2.10.x,额外的一行样板,因为隐式解析是——好吧,我们不会说它坏了,我们只会说它不起作用.

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
}

推广评论:

@WilfredSpringer 有人听到了你的声音.SI-6967

@WilfredSpringer Someone heard you. SI-6967

这篇关于基于 ClassTag 的模式匹配对于基元失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆