gpt4 book ai didi

用于模式匹配的 Scala 类型删除

转载 作者:行者123 更新时间:2023-12-04 17:53:37 26 4
gpt4 key购买 nike

我一直在论坛和 Google 上搜索有关 Scala 类型删除问题的答案。但是,我找不到任何可以回答我的问题的内容。

我在与 ParamClass 的类型参数匹配的对象上努力进行模式匹配。我需要将传入对象的类型模式匹配到 bar 方法。我已经看到了解决方案,例如

bar[X](a : X)(implicit m : Manifest[X])

这将解决我的问题,但我不能使用它,因为 bar 方法是一个被覆盖的方法。 (其实就是Akkaactor框架中的receive偏函数)。下面给出了代码,应该是不言自明的:
class ParamClass[A : Manifest] {
def bar(x : Any) = x match {
case a: A => println("Found A: " + a)
case _ => println("No match: " + x)
}
}

object ErasureIssue {
def main(args: Array[String]) {
val clz = new ParamClass[Int]
clz.bar("faf")
clz.bar(2.3)
clz.bar(12) // this should match, but does not
}
}

ErasureIssue.main(null)

非常感谢解决此问题的任何帮助。我正在使用 Scala 2.9.1,顺便说一句。

-J

最佳答案

理论上可以签到bar像这样:x.getClass == implicitly[Manifest[A]].erasure ,但对于诸如 Int 之类的原始类型失败了 list 正确删除到 Int ,但是 bar用盒装类型调用 java.lang.Integer ... :-(

您可能需要 A成为 AnyRef为了获得盒装 list :

class ParamClass[A <: AnyRef : Manifest] {
def bar(x : Any) = x match {
case _ if x.getClass == implicitly[Manifest[A]].erasure =>
println("Found A: " + x.asInstanceOf[A])
case _ => println("No match: " + x)
}
}

object ErasureIssue {
def main(args: Array[String]) {
val clz = new ParamClass[Integer] // not pretty...
clz.bar("faf")
clz.bar(2.3)
clz.bar(12) // ok
}
}

ErasureIssue.main(null)

鉴于您需要构造原始数组,您可以直接存储装箱类,独立于未装箱 list :
object ParamClass {
def apply[A](implicit mf: Manifest[A]) = {
val clazz = mf match {
case Manifest.Int => classOf[java.lang.Integer] // boxed!
case Manifest.Boolean => classOf[java.lang.Boolean]
case _ => mf.erasure
}
new ParamClass[A](clazz)
}
}
class ParamClass[A] private[ParamClass](clazz: Class[_])(implicit mf: Manifest[A]) {
def bar(x : Any) = x match {
case _ if x.getClass == clazz =>
println("Found A: " + x.asInstanceOf[A])
case _ => println("No match: " + x)
}

def newArray(size: Int) = new Array[A](size)

override def toString = "ParamClass[" + mf + "]"
}

val pi = ParamClass[Int]
pi.bar("faf")
pi.bar(12)
pi.newArray(4)

val ps = ParamClass[String]
ps.bar("faf")
ps.bar(12)
ps.newArray(4)

关于用于模式匹配的 Scala 类型删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11864483/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com