I am curious:
scala> Some(null) == None res10: Boolean = false
Why isn't Some(null)
transformed to None
?
Below are some important points about null in java that every Java programmer should know: 1. null is Case sensitive: null is literal in Java and because keywords are case-sensitive in java, we can't write NULL or 0 as in C language.
Null is - together with scala. Nothing - at the bottom of the Scala type hierarchy. Null is the type of the null literal. It is a subtype of every type except those of value classes. Value classes are subclasses of AnyVal, which includes primitive types such as Int, Boolean, and user-defined value classes.
Scala some class returns some value if the object is not null, it is the child class of option. Basically, the option is a data structure which means it can return some value or None. The option has two cases with it, None and Some. We can use this with the collection.
Null serves as the default value of any uninitialized reference variable, including instance variables and static variables (although you will still receive a compiler warning for uninitialized local variables).
You should use Option(null)
to reach the desired effect and return None
.
Some(null)
just creates a new Option
with a defined value (hence Some
) which is actually null
, and there are few valid reasons to ever create one like this in real code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With