DEV Community

Discussion on: Save the Optional, stop using isPresent

Collapse
 
alebianco profile image
Alessandro Bianco

I find the 'exercise for the user' quite confusing since no clear signature for those methods are given and the return type changes between the examples.

Anyway, if anyone is wondering, flatMap is used because getAddress returns an Option<Address> in one of the examples. map is used instead with 'getStreet' because it returns a plain String.

Collapse
 
koenighotze profile image
David Schmitz

Good point, thank you. I have added a small extra sentence, that hopefully reduced the confusion.

To add to your answer: flatMap expects a mapper that returns an Option. map on the other hand wraps the result of the applied mapper function into either some or none.

In short: map creates a new "Gift Box" and flatMap reuses the "Gift Box".