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

Jérôme M. Berger <[email protected]> Sun, 17 Apr 2016 19:27:40 +0200
Newsgroups gmane.comp.lang.scala
Message-ID <[email protected]>
On 04/17/2016 06:27 PM, Marc Arndt wrote:
> 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.
> 
	In that case, you want to use flatMap instead of map. This allows you
to be explicit about the possibly null value:
Option(a).flatMap(Option(_.getB)).map(_.isValid)

		Jerome
-- 
mailto:[email protected]
http://jeberger.free.fr
Jabber: [email protected]

-- 
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.
signature.asc (application/pgp-signature, 181 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iEYEARECAAYFAlcTx4wACgkQd0kWM4JG3k+/TQCfU+ESOUryFdTQ4AyomifuUV4F
uBgAoJ3z8Ok0T8Z3n9oUaaZD+hQ9JRCf
=bquL
-----END PGP SIGNATURE-----