【发布时间】:2010-01-29 20:16:46
【问题描述】:
任何人都可以重写这段代码来做同样的事情,但没有任何编译器警告:-
object TestTypeErasure {
def main(args:Array[String]) {
def myFunction(myObject:Any):Boolean = {
true
}
val myVariable: (Any => Boolean) = myFunction
myVariable match {
case function:(Any => Boolean) => println("Match")
}
}
}
万分感谢
基思
更新!!!!抱歉,对此进行了真正的散列。这是我关于 SO 的第一个问题
只是为了让大师知道我已经尝试过类似的方法也无济于事:-(无法编译)
object TestTypeErasure {
def doTest(parameter: Any) = {
parameter match {
case function:Function1[_, _] => function("Whatever")
}
}
def main(args:Array[String]) {
}
}
我得到错误:-
TestTypeErasure.scala:6: error: type mismatch;
found : java.lang.String("Whatever")
required: _
case function:Function1[_, _] => function("Whatever")
^
one error found
再次感谢
基思
【问题讨论】:
标签: scala types pattern-matching compiler-warnings type-erasure