加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 将元组与null匹配

发布时间:2020-12-16 19:11:25 所属栏目:安全 来源:网络整理
导读:我不明白为什么以下情况不匹配. 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 谢谢. 解决方法 这
我不明白为什么以下情况不匹配. 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

谢谢.

解决方法

这完全符合规定.

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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读