将元组与null匹配

agi*_*all 6 null scala tuples pattern-matching

我不明白为什么以下情况不匹配.Null应该是Any的实例,但它不匹配.有人可以解释发生了什么吗?

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
Run Code Online (Sandbox Code Playgroud)

谢谢.

psp*_*psp 9

这完全符合规定.

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.
Run Code Online (Sandbox Code Playgroud)

有趣的是,如此多的相关性归因于null是Any的成员.它是各种类型的成员,但AnyVal和Nothing.


oxb*_*kes 6

您是否尝试过的v占位符什么

val x = (2, null)
x match {
    case (i:Int, v) => println("got tuple %s: %s".format(i, v))
    case _ => println("catch all")
}
Run Code Online (Sandbox Code Playgroud)