Scala Some v. Option
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27626237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scala Some v. Option
提问by Kevin Meredith
What's the difference between Someand Option?
什么之间的区别Some和Option?
scala> Some(true)
res2: Some[Boolean] = Some(true)
scala> val x: Option[Boolean] = Some(true)
x: Option[Boolean] = Some(true)
scala> res2 == x
res3: Boolean = true
I see that Option(null)returns, whereas Some(null)won't compile:
我看到Option(null)返回,而Some(null)不会编译:
scala> val x = Option(null)
x: Option[Null] = None
scala> val x: Option[Boolean] = Some(null)
<console>:7: error: an expression of type Null is ineligible for implicit conversion
val x: Option[Boolean] = Some(null)
^
采纳答案by Michael Zajac
Well, Someextends Option, so it inherits everything except getand isEmpty(and some other methods implemented by a case class).
好吧,Someextends Option,所以它继承了除getand之外的所有内容isEmpty(以及由 case 类实现的其他一些方法)。
The companion object of Optionhas a special applymethod for dealing with null:
的伴生对象Option有一个特殊apply的处理方法null:
def apply[A](x: A): Option[A] = if (x == null) None else Some(x)
But Some.applyis just the standard applymethod generated for a case class.
但这Some.apply只是apply为案例类生成的标准方法。
Some(null)willcompile in some circumstances, but it has type Some[Null](or Option[Null]), which can only be assigned when the type argument of Optionis a reference type.
Some(null)在某些情况下会编译,但它有类型Some[Null](或Option[Null]),只有当 of 的类型参数Option是引用类型时才能赋值。
scala> val a = Some(null)
a: Some[Null] = Some(null)
scala> val b: Option[String] = Some(null)
b: Option[String] = Some(null)
You're trying to assign a Some[Null]to an Option[Boolean], but a Nullis not a sub-type of Boolean, because Booleanis a value type (primitive underneath) and cannot hold a value of null.
您试图将 a 分配Some[Null]给 an Option[Boolean],但 aNull不是 的子类型Boolean,因为Boolean是值类型(下面的原始类型)并且不能保存 的值null。
回答by Ashish
In short
简而言之
Option
/ \
/ \
/ \
Some None
Option is container base which can be empty or full
选项是容器底座,可以是空的或满的
While Some(x) represent full with 'x' being present in the container, None represents empty.
Some(x) 表示容器中存在 'x' 时已满,而 None 表示空。
回答by Vladimir Gapeyev
From the functional programming perspective, given an arbitrary type T, the type Option[T] is an algebraic datatype with data constructors None and Some(x:T).
This is merely a coded way of saying that, if the type T consists of values t1, t2, t3, ...
then allthe values of the type Option[T] are None, Some(t1), Some(t2), Some(t3), ...
从函数式编程的角度来看,给定任意类型 T,类型 Option[T] 是具有数据构造函数 None 和 Some(x:T) 的代数数据类型。
这只是一种编码方式,如果类型 T 由值 t1, t2, t3, ... 组成,则Option[T] 类型的所有值都是 None, Some(t1), Some(t2),一些(t3),...
Most everything else follows from this. E.g., if null is not a value of T, then Some(null) is not a value of Option[T]. This explains why
大多数其他的一切都来自于此。例如,如果 null 不是 T 的值,则 Some(null) 不是 Option[T] 的值。这解释了为什么
val x: Option[Boolean] = Some(null)
does not work, while
不起作用,而
val x: Option[Null] = Some(null)
does.
做。
Finally, specifically to Scala, there seems to be an additional quirk in that, "for convenience" one may say Option(null) when they mean None. I'd expect one can also say Option(t) when they mean Some(t).
最后,特别是对于 Scala,似乎还有一个额外的怪癖,“为了方便”,当它们表示 None 时,人们可能会说 Option(null)。我希望人们在表示 Some(t) 时也可以说 Option(t)。
回答by Matthew Reece
The basic idea of Option is to eliminate using Null in order have a default value and avoid a NullPointerException. So Option can have Some value or None (replacing Null). Check out this great explanation of Option: Scala Option explained
Option 的基本思想是消除使用 Null 以获得默认值并避免 NullPointerException。所以 Option 可以有 Some value 或 None (替换 Null)。查看 Option 的这个很好的解释: Scala Option 解释

