What\'s the difference between Some
and Option
?
scala> Some(true)
res2: Some[Boolean] = Some(true)
scala> val x: Option[Boolean]
Well, Some
extends Option
, so it inherits everything except get
and isEmpty
(and some other methods implemented by a case class).
The companion object of Option
has a special apply
method for dealing with null
:
def apply[A](x: A): Option[A] = if (x == null) None else Some(x)
But Some.apply
is just the standard apply
method generated for a case class.
Some(null)
will compile in some circumstances, but it has type Some[Null]
(or Option[Null]
), which can only be assigned when the type argument of Option
is a reference type.
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 Null
is not a sub-type of Boolean
, because Boolean
is a value type (primitive underneath) and cannot hold a value of null
.