为什么 Some(null) 不被视为 None?
我很好奇:
scala> Some(null) == None
res10: Boolean = false
为什么 Some(null)
不转换为 None
?
I am curious:
scala> Some(null) == None
res10: Boolean = false
Why isn't Some(null)
transformed to None
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您应该使用
Option(null)
来达到所需的效果并返回None
。Some(null)
只是创建一个具有定义值(因此是Some
)的新Option
,该值实际上是null
,并且几乎没有正当理由在实际代码中创建这样的代码。You should use
Option(null)
to reach the desired effect and returnNone
.Some(null)
just creates a newOption
with a defined value (henceSome
) which is actuallynull
, and there are few valid reasons to ever create one like this in real code.不幸的是,
null
对于任何AnyRef
类型来说都是有效值——这是 Scala 与 Java 互操作性的结果。因此,如果一个方法采用A
类型的对象,并在内部将其存储在Option
中,则很可能需要在其中存储null
选项。例如,假设您有一个方法,它获取列表的头部,检查该头部是否对应于存储中的键,如果是则返回 true。人们可能会这样实现它:
所以,事情是这样的...如果
list
和keys
中的内容来自某个 Java API,它们很可能都包含空!如果
Some(null)
不可能,则isFirstAcceptable(List[String](null), Set[String](null))
将返回false< /code> 而不是
true
。Unfortunately,
null
is a valid value for anyAnyRef
type -- a consequence of Scala's interoperability with Java. So a method that takes an object of typeA
and, internally, store it inside anOption
, might well need to store anull
inside that option.For example, let's say you have a method that takes the head of a list, checks if that head correspond to a key in a store, and then return true if it is. One might implement it like this:
So, here's the thing... if the that inside
list
andkeys
come from some Java API, they both may well containnull
! IfSome(null)
wasn't possible, thenisFirstAcceptable(List[String](null), Set[String](null))
would returnfalse
instead oftrue
.我认为线程中的其他人很好地解释了为什么
Some(null)
“应该”存在,但是如果您碰巧在某个地方得到Some(null)
并且想要将其转换为None
的快速方法,我之前已经这样做过:当起始
Option
是合法的非空值时,事情会按照您可能想要的方式工作:I think the others in the thread do a good job explaining why
Some(null)
"should" exist, but if you happen to be gettingSome(null)
somewhere and want a quick way to turn it intoNone
, I've done this before:And when the starting
Option
is a legit non-null value things work as you probably want:Scala 的大部分问题都可以归因于它与 Java 兼容的需要。
null
在 Java 中经常用作值,表示可能不存在值。例如,如果键不匹配,hashMap.get(key)
将返回null
。考虑到这一点,请考虑将 null 返回方法包装在
Option
中的以下可能值:Some(null)
似乎与None
完全不同在这种情况下,需要保证在该语言中允许它。当然,如果您不希望这样做,那么只需使用:
Much of Scala's WTFs can be attributed to its need for compatibility with Java.
null
is often used in Java as a value, indicating, perhaps the absence of a value. For examplehashMap.get(key)
will returnnull
if the key is not matched.With this in mind, consider the following possible values from wrapping a null returning method in an
Option
:Some(null)
seems sufficiently distinct fromNone
in this case to warrant allowing it in the language.Of course if this is not desirable in your case then simply use:
作为一个简单的思想实验,考虑两个字符串列表,一个长度为 5,另一个长度为 20。
因为我们在 JVM 上运行,所以可以将
null
作为有效元素插入到其中之一这些列表 - 因此将其作为元素 #10 放入长列表中那么,以下两个表达式返回的值应该有什么区别?
编辑:将
get
交换为lift
,我正在考虑地图......As a simple thought experiment, consider two lists of Strings, one of length 5 and one of length 20.
Because we're running on the JVM, it's possible to insert
null
as a valid element into one of these lists - so put that in the long list as element #10What, then, should the difference be in the values returned from the two following expressions?
EDIT: Exchanged
get
forlift
, I was thinking of maps...因为 Option 被认为是 Functor,并且作为 Functor 意味着:
unit
函数(apply
或只是Option("blah")
)map
函数,可从T=>B
转换值,但不遵循上下文在本主题中,主要部分是 #2 -
Option(1).map(t=>null)
无法转换上下文。一些
应该保留。否则它会破坏结合律!只需考虑以下定律示例:
但是如果
Option("hello").map(t=>null)
产生None
会怎样?结合律将被打破:这是我的想法,可能是错误的
Because Option is considered to be a Functor and being a Functor means:
unit
function (apply
or justOption("blah")
in Scala)map
function which transforms value fromT=>B
but not a contextIn this topic the main part is #2 -
Option(1).map(t=>null)
can not transform context.Some
should remain. Otherwise it brakes associative law!Just consider the following laws example:
But what if
Option("hello").map(t=>null)
producedNone
? Associative law would be broken:That is my thought, might be wrong