On Tue, Oct 30, 2012 at 6:58 PM, Josh Berry <[email protected]> wrote:

> What is wrong with pattern matching on the content of an option?


It's basically equivalent to testing against null: if there's an object, do
this, otherwise, do that.

It's sometimes (often?) unavoidable when you are dealing with API's that
take raw values instead of Options, but you do want to weave through your
monads as much as possible (flatMap) to reap all the benefits that Option
has to offer: instead of extracting the value inside the monad and
operating on it, consider lifting your operation so it can operate on the
monad directly.

Here is an example of someone doing it
wrong<http://blog.danielwellman.com/2008/03/using-scalas-op.html> (I
explained why in this
article<http://beust.com/weblog/2010/07/28/why-scalas-option-and-haskells-maybe-types-wont-save-you-from-null/>).
"Doing it wrong" is probably a bit harsh, the kind of code in this article
is common when you are just discovering Option and you haven't fully
realized its potential. It's a lot more than a container, although I agree
that explaining it as "a list of zero or one element" is a good
introduction to give an idea of what it is.

-- 
Cédric

-- 
You received this message because you are subscribed to the Google Groups "Java 
Posse" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/javaposse?hl=en.

Reply via email to