match tuple with null
I don't understand why the following case doesn't match. Null should be an instance of Any, but it doesn't match. Can someone explain what is going on?
val x = (2, null) x match { case (i:Int, v:Any) => println("got tuple %s: %s".format(i, v)) case _ => println("catch all") } prints catch all
Thanks.
Answers
This is exactly as specified.
Type patterns consist of types, type variables, and wildcards. A type pattern T is of one of the following forms: * A reference to a class C, p.C, or T#C. This type pattern matches any non-null instance of the given class.
It's interesting that so much relevance has been attributed to null being a member of Any. It's a member of every type but AnyVal and Nothing.