Re: Different behaviour from Option map in scala compared to Optional map in java
Sébastien Doeraene <[email protected]> Sun, 17 Apr 2016 18:36:14 +0200
| Newsgroups | gmane.comp.lang.scala |
|---|---|
| Message-ID | <CAJwkOg4LuY-MEL5ZYumuwose1zpbFD_aDf1wp68uysxBNZTwrg@mail.gmail.com> |
Hello, There are good reasons that .map() does not change `null` to `None`. If it did that, I would never be able to use a `.map` on an `Option[T]` where I myself do not know what T will be (typically, because I'm writing a library). The user of my library might very well use my library with a T that can reasonably contain null values. In the insides of my library, using a null-handling .map() would *destroy* the data of my user. In other words, such a map would not be completely *type parametric*. If you want a .map() that takes care of `null` returned by its function, you can use `.flatMap(x => Option(f(x)))`. Cheers, Sébastien On Sun, Apr 17, 2016 at 6:30 PM, Vlad Patryshev <[email protected]> wrote: > I agree; Some(null) is an abomination, and Option(f(this.get)) would make > more sense. > > Actually, in my class Result I wrap all these things (exceptions and > nulls) even for filter and collect, not just map. Meaning, if the predicate > called in filter() throws an exception, that's a legit error, and should be > wrapped as Bad(exception). I think, David Pollak's Box does the same. > > Thanks, > -Vlad > > On Sun, Apr 17, 2016 at 3:27 AM, Marc Arndt <[email protected]> wrote: > >> Hello, >> while programming with java 8 and scala I discovered that both seem to >> have a different behaviour with the method map in the classes Option in >> scala and Optional in java. >> 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. >> >> In java the method map in Optional is implemented as followed: >> >> >> >> >> >> >> >> * public<U> Optional<U> map(Function<? super T, ? extends U> mapper) >> { Objects.requireNonNull(mapper); if >> (!isPresent()) return empty(); else { return >> Optional.ofNullable(mapper.apply(value)); } }* >> This method does not require, that the given method returns a value not >> null, it is also accepted to return null. >> >> I'm not sure if this behaviour is intended. >> >> Many Greetings >> Marc >> >> -- >> 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. > -- 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.