Re: Different behaviour from Option map in scala compared to Optional map in java

Marc Arndt <[email protected]> Sun, 17 Apr 2016 09:27:35 -0700 (PDT)
Newsgroups gmane.comp.lang.scala
Message-ID <[email protected]>
Hi,
you're right it's not a bug, but I normally think that getting a Some 
object means that it does not contain null, because null would be 
represented with None.
This thinking leads to a NullPointerException when I try to use the 
contained value in the returned Some object.
This is strange in my eyes because I would use the map method from Option 
to prevent getting NullPointerExceptions.
Therefore I think it's more intuitive if map returns Option(f(this.get)) 
instead of Some(f(this.get)), because this would prevent a 
NullPointerException if I use the returned Option object from map.

I'm asking, because I often use the map method from Option to wrap a 
variable from an java object, which can be null, into an Option object, 
which makes it possible to use map chains like: Option(a).map( _.getB 
).map( _.isValid )
This is especially useful when I use java libraries like hibernate to 
persist objects and their relations in a database and also want to use them 
in scala.

Greetings
Marc

Am Sonntag, 17. April 2016 18:08:32 UTC+2 schrieb David Starner:
>
> On Sun, Apr 17, 2016 at 3:27 AM Marc Arndt <[email protected] 
> <javascript:>> wrote:
>
>> In scala the method map in Option is implemented as followed:
>>
>>
>> *  @inline final def map[B](f: A => B): Option[B] =    if (isEmpty) None 
>> else Some(f(this.get))*This method requires, that f doesn't return null.
>>
>
> That just means that if f returns null, the map returns Some(null).
>  
>
>> I'm not sure if this behaviour is intended.
>>
>
> Scala treats null generally as being something that is supported for 
> legacy Java-compatibility reasons only. In that context, it makes perfect 
> sense to just pass through the null here. Java 8's choice may be reasonable 
> in its context, but less so for Scala. 
>

-- 
You received this message because you are subscribed to the Google Groups "scala-language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/d/optout.