Not sure if this is an appropriate question for here, please let me know!
In Scala, the ever so useful Option
class has an apply
method in its companion object that allows us to quickly wrap any value into an Option
:
def apply[A](x: A): Option[A] = if (x == null) None else Some(x)
This is super helpful when dealing with mixed Java/Scala code. As you can wrap null
in an Option
and get back a Scala friendly None
. However, if you do Option(None)
you receive a Some(None)
, when perhaps you would expect to receive a None
back.
My question is why wasn't the design of that method something more like:
def apply[A](x: A): Option[A] = if (x == null || x == None) None else Some(x)
Does Some(None)
make more sense in some scenarios? I would like the second approach to do something like:
val isNull = Option(someValue).isEmpty // returns false for Some(None)
Option()
is applied to nullable values. Are you saying that you are passing in a nullableOption
value? If so, rethink your coding practices.