Re: Different behaviour from Option map in scala compared to Optional map in java
Vlad Patryshev <[email protected]> Sun, 17 Apr 2016 09:30:38 -0700
| Newsgroups | gmane.comp.lang.scala |
|---|---|
| Message-ID | <CAFhNTo8orUsubRcnZfq0kSigPK0O+A6tEV81PL3g4r2kBX3hDw@mail.gmail.com> |
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.