将元组与空匹配 [英] match tuple with null

查看:64
本文介绍了将元组与空匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

谢谢.

推荐答案

这完全符合规定.

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.

有趣的是,null 是 Any 的成员,因此相关性如此之高.它是除 AnyVal 和 Nothing 之外的所有类型的成员.

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.

这篇关于将元组与空匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆