Re: Different behaviour from Option map in scala compared to Optional map in java
Naftoli Gugenheim <[email protected]> Sun, 17 Apr 2016 16:44:32 +0000
| Newsgroups | gmane.comp.lang.scala |
|---|---|
| Message-ID | <CANpg8PDBczLzR1HbyKjt-2dZKVdaAuaSDaLcN9kMxsqKcWGh+g@mail.gmail.com> |
On Sun, Apr 17, 2016, 12:30 PM Vlad Patryshev <[email protected]> wrote: > I agree; Some(null) is an abomination, > Then don't return null in the function you pass map! But if you do return null that's your choice, it makes zero sense for map to steamroll over your choice. 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.