Reputation: 169
Let's say I have defined a function as:
f:(Int)=>String
Why does the following result in error?
Option(null).map(f)
While the following just works fine?
None.map(f)
PS: Option(null)
evaluates to None
Edit: Following is the error I see on scala console
scala> Option(null).map(f)
<console>:14: error: type mismatch;
found : Int => String
required: Null => ?
Option(null).map(f)
Upvotes: 1
Views: 119
Reputation: 15086
The expression Option(null)
has type Option[Null]
, while None
is an instance of Option[Nothing]
. Null
is not a subtype of Int
, but Nothing
is.
Option(null)
evaluates to None
but that is not known at compile time. It doesn't make any sense to literally write Option(null)
anyway. That construct is only useful when you're not sure whether the argument is null
or not.
Upvotes: 6