This article makes it seem like you'd have to explicitly check whether a Maybe value is Nothing when you use it. This is certainly <i>safe</i>, but it's also very awkward; as a contrived example, adding two numbers would look like this:<p><pre><code> case a of
Nothing -> Nothing
Just a -> case b of
Nothing -> Nothing
Just b -> a + b
</code></pre>
This is quite a bit of boilerplate hiding the expression that actually matters--a + b! Moreover, whenever you have code that creeps steadily to the right, it means you either messed up or missed an abstraction.<p>It turns out that this pattern--do a computation if all the values are present, but return Nothing if <i>any</i> of them are Nothing--happens very often. Happily, we can get some nice syntax for Maybe computations like this using do-notation in Haskell or for-comprehensions in Scala:<p><pre><code> do a <- a
b <- b
return (a + b)
</code></pre>
This is much better! It makes even more sense for more complicated expressions, especially when later results depend on values of earlier ones. However, for a simple example like a + b, it's still quite a bit of boiler plate; we can certainly do better! Here are two alternatives using functions from the Control.Applicative module:<p><pre><code> (+) <$> a <*> b
liftA2 (+) a b
</code></pre>
The important idea here is that both versions somehow "lift" the (+) function to work over Maybe values. This just means they create a new function with checks for Maybe built in. This is great because it saves all the boilerplate above and nicely abstracts away most of the null checks while preserving safety. But the syntax is still a bit awkward. Happily, if you don't mind using a preprocessor[1], you can get some very nice syntax called "idiom brackets":<p><pre><code> (| a + b |)
</code></pre>
[1]: <a href="https://personal.cis.strath.ac.uk/conor.mcbride/pub/she/" rel="nofollow">https://personal.cis.strath.ac.uk/conor.mcbride/pub/she/</a><p>The computation inside the (| and |) is lifted over Maybe, just like the two previous examples. I think this is the clearest option here: it has the least syntactic overhead, and the base expression--a + b--is very easy to read. They also have the advantage of nesting, so you can express a + b + c, where you want a null check for all three variables, as:<p><pre><code> (|(|a + b|) + c|)
</code></pre>
This isn't <i>perfect</i>, but I think it's still very easy to follow. It might be better if the (| and |) were a single character, something like this:<p><pre><code> ⦇⦇a + b⦈ + c⦈
</code></pre>
However, some people really don't like Unicode symbols in their code :(. Happily, you can have the source look like (|foo|) and have Emacs replace it with ⦇foo⦈ without actually changing the code. It's basically Unicode syntax highlighting. I think this leads to the most readable code so far.<p>So my main point is that you can abstract out the common case where you check for Nothing and make the whole expression Nothing if any sub-expression is. This saves quite a bit of typing and <i>much</i> more importantly makes the resulting code far easier to read.<p>Another really cool part is that all these syntax forms and functions are <i>not</i> specific to Maybe--they actually work for a whole bunch of different types. So you would not be bloating your language by including special features just for safely checking nulls; these features are much more general.