match issue

11 views
Skip to first unread message

Konstantine Kougios

unread,
Sep 10, 2011, 5:44:54 PM9/10/11
to scala...@googlegroups.com
Hi,

    I came up to this weird thingy:

scala> val f =(x:Any,y:Any)=> x match {                                          
     | case _: Int | Boolean | Long | Float | Double | Byte | Short | Char => x==y
     | }
f: (Any, Any) => Boolean = <function2>

scala> f(5d,5d)
scala.MatchError: 5.0
    at $anonfun$1.apply(<console>:5)
    at $anonfun$1.apply(<console>:5)
    at .<init>(<console>:7)
    at .<clinit>(<console>)
    at RequestResult$.<init>(<console>:9)
    at RequestResult$.<clinit>(<console>)
    at RequestResult$scala_repl_result(<console>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at scala.tools.nsc.Interpreter$Request$$anonfun$loadAndRun$1$$anonfun$apply$17.apply(Interpreter.scala:988)
    at scala.tools.nsc.Interpreter$Request$$anonfun$loadAndRun$1$$anonfun$apply$17.apply(Interpreter.scala:988)
    at scala.util.control.Ex...



Why doesn't it match the double value?

Cheers

Roland Kuhn

unread,
Sep 10, 2011, 6:16:20 PM9/10/11
to Konstantine Kougios, scala...@googlegroups.com
Hi Konstantine,

you might have been tricked into thinking that you included the type Double in the pattern match, but you actually have only object Double there. Each one of the | separated sub-patterns must be complete by itself, i.e. include _: in your case.

Regards,

Roland

Roland Kuhn
Typesafe – Enterprise-Grade Scala from the Experts
twitter: @rolandkuhn

Konstantine Kougios

unread,
Sep 10, 2011, 6:44:59 PM9/10/11
to Roland Kuhn, scala...@googlegroups.com
Ok, I see, thanks
Reply all
Reply to author
Forward
0 new messages