def test1(a: Any) = a match { case x: AnyRef => "AnyRef" case _ => "None of the above" } def test2(a: Any) = a match { case x: Double if x > 2 => "Double > 2" case x: AnyRef => "AnyRef" case _ => "None of the above" } Please can someone explain why in the following, the first case 1.0 matches on AnyRef, but in the second it doesn't. (Scala 2.9.0-1)
scala> test1(1.0) res28: java.lang.String = AnyRef scala> test2(1.0) res29: java.lang.String = None of the above edit - Scala 2.10 update Jan 2013: the new pattern matcher fixes this behaviour (or at least, makes it consistent) and the method test2 now returns "AnyRef" as for test1.
1.0is aDoublewhich is a subtype ofAnybut not ofAnyRefin contrast tojava.lang.Double. So I even wonder why1.0matchesAnyRefintest1.AnyRef. Rex Kerr is correct.