【发布时间】:2012-08-08 12:25:10
【问题描述】:
我一直在论坛和 Google 上搜索有关 Scala 类型擦除问题的答案。但是,我找不到任何可以回答我的问题的内容。
我在与 ParamClass 的类型参数匹配的对象上进行模式匹配。我需要对 bar 方法的传入对象类型进行模式匹配。我见过诸如
之类的解决方案bar[X](a : X)(implicit m : Manifest[X])
这可以解决我的问题,但我不能使用它,因为 bar 方法是一种被覆盖的方法。 (其实就是Akka actor框架中的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
【问题讨论】:
标签: scala pattern-matching type-erasure