这是Scala 2.8.0中的错误吗? (与2.8.1.RC2同样的情况)(数字,数字)匹配(浮点数,整数)但不匹配(Int,浮点数)
import junit.framework._ import Assert._ class BugTest extends TestCase { def compare(first: Any, second: Any): Int = { (first, second) match { case (k: Int, o: Int) => k compare o //why the next case matches (Float, Int) but does not match (Int, Float) ??? case (k: Number, o: Number) => k.doubleValue() compare o.doubleValue() case _ => throw new Exception("Unsupported compare " + first + "; " + second) } } def testCompare() { assertEquals("Both Int", -1, compare(0, 1)) assertEquals("Both Float", 1, compare(1.0, 0.0)) assertEquals("Float then Int", 0, compare(10.0, 10)) assertEquals("Int then Float", 0, compare(10, 10.0))//this fails with an exception } }
这是#1697 /#2337的一种表现形式。请参阅http://lampsvn.epfl.ch/trac/scala/ticket/2337。 – extempore 2010-10-13 06:46:40
啊,很高兴知道,谢谢! – 2010-10-13 06:50:26
谢谢。我已经添加了这个作为票的另一个例子。 – Andrey 2010-10-13 08:34:56