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

Naftoli Gugenheim <[email protected]> Sun, 17 Apr 2016 16:42:15 +0000
Newsgroups gmane.comp.lang.scala
Message-ID <CANpg8PCXcNOwJYf0h7+xt-rK_efKLbbAgoSkStk9A-K7_mp68g@mail.gmail.com>
On Sun, Apr 17, 2016, 12:27 PM Marc Arndt <[email protected]> wrote:

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

That's not a valid assumption. There's nothing stopping you from writing
Some(null) anywhere. It course it's probably not a good idea, but it's no
better or worse than writing null on general.
It's true that *one* of the benefits of Option is to eliminate the need for
null, but at the end of the day it's just a tool, and it's up to you how to
use it.

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

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