Shubham Verma shows us what happens when we use the
flatMap() functions in Scala on fields which are marked as Options or Futures:
now let’s move towards the interesting part flatMap(), what it is supposed to do in case of Option so the flatMap() gives us the liberty to return the type of value that we want to return after the transformation, unlike map() in wherein when a parameter has the value Some the value would be of type Some what so ever, but its not with the case with flatMap()scala> option.flatMap(x => None) res13: Option[Nothing] = None scala> scala> option.map(x => None) res14: Option[None.type] = Some(None)
the code snippet above clearly shows it, so is that it ? No not yet lets look on to one more feature of
Option[+A]that comes to be real handy when we need to extract value out of options, supposedly we have list of type
List[Option[Int]]now I am only interested in values that have some value which seems to be an obvious usecase in most of the times, we can simply do it using a
In short, it’s a little more complex, but you can still get useful information.