gpt4 book ai didi

scala - 这在 Scala 中可以为 null 吗?

转载 作者:行者123 更新时间:2023-12-04 10:42:12 25 4
gpt4 key购买 nike

我刚看完 this question并偶然发现以下引用:

Scala treats == as if it were defined as follows in class Any:

final def == (that: Any): Boolean = 
if (null eq this) (null eq that) else (this equals that)

(null eq this)部分让我想知道:真的可以在空指针上调用方法吗?可以 thisnull在斯卡拉?

最佳答案

查看 Scala language specification ,即 6.3 空值章节:

The null value is of type scala.Null, and is thus compatible with every reference type. It denotes a reference value which refers to a special “null” object. This object implements methods in class scala.AnyRef as follows:

eq(x) and ==(x) return true if the argument x is also the “null” object.

ne(x) and !=(x) return true if the argument x is not also the “null” object.



这意味着在语义上,当您将某些内容与 null 进行比较时文字或 null文字与你实际上指的特殊方法 scala.Null 类(class)。治疗 null字面量作为该类的简写。

当然在实现层面是优化的,普通的 null用来。

关于scala - 这在 Scala 中可以为 null 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10072645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com