The many methods of `Option`

The Option type has a lot of convenience methods for quickly manipulating the value while leaving it in the Option, providing defaults, and so on. Jason Orendorff and I were trying to get a grip on the naming conventions they follow, and produced this chart:

The chart is a little hard to get your head around, but once you get it, I think it's pretty great. Find the method you want; the column and row headings tell you what it does.

One reason I like it is that it shows why I really hate the names of map_or and map_or_else: they return the result of the closure call, not a Some variant holding the result, The ordinary map gets it right, following the precedent set by Haskell (and, I think, other languages?).

If you'd like to see the chart in its own page, it's a Google doc.

2 Likes

I found map_or's return type surprising at first, but now I am very glad to have it; I would be unwrapping the result a lot more than I currently am wrapping it if it returned a (totally spurious) option.

I agree it's useful behavior; I just meant to point out the problem with its name.

All these methods are stable, so I'm not really agitating for any particular change; I just think it's nice to get the whole story laid out.

Agreed. The chart is cool :smiley: