Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala Some v. Option

Tags:

scala

What's the difference between Some and 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:

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)
                                     ^
like image 537
Kevin Meredith Avatar asked Dec 23 '14 18:12

Kevin Meredith


People also ask

What is option and some in scala?

An Option[T] can be either Some[T] or None object, which represents a missing value. For instance, the get method of Scala's Map produces Some(value) if a value corresponding to a given key has been found, or None if the given key is not defined in the Map.

What is option () in scala?

The Option in Scala is referred to a carrier of single or no element for a stated type. When a method returns a value which can even be null then Option is utilized i.e, the method defined returns an instance of an Option, in place of returning a single object or a null.

What is getOrElse in scala?

As we know getOrElse method is the member function of Option class in scala. This method is used to return an optional value. This option can contain two objects first is Some and another one is None in scala. Some class represent some value and None is represent a not defined value.

What is isDefined in scala?

Definition Classes IterableOps.  final def isDefined: Boolean. Returns true if the option is an instance of scala. Some, false otherwise. Returns true if the option is an instance of scala.Some, false otherwise.


3 Answers

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.

like image 143
Michael Zajac Avatar answered Oct 19 '22 18:10

Michael Zajac


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.

like image 31
Ashish Avatar answered Oct 19 '22 18:10

Ashish


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 all the values of the type Option[T] are None, Some(t1), Some(t2), Some(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

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).

like image 19
Vladimir Gapeyev Avatar answered Oct 19 '22 18:10

Vladimir Gapeyev