[Haskell-cafe] Error-containing Maybes

Ivan Lazar Miljenovic ivan.miljenovic at gmail.com
Thu Jun 15 09:38:22 UTC 2017


The Maybe type is often (almost always?) used to indicate that a
computation may fail; specifically, `Maybe a` typically indicates that
it may not be possible to always produce a value of type `a`.

However, there are times when the inverse of this is desirable
(usually within a monadic/side-effectful context): if Nothing is
returned then the computation was a success; otherwise, an error
message is returned.  Whilst this usage definitely fits the types, it
breaks the mould of how we think/use Maybe in most cases and may at
times cause confusion.  As such, it may be advisable (and I know I've
done this at least once) to define a `data Result e = Error e |
Success` data type to model this behaviour.

Is this a typical enough requirement that would make it worth adding
such a datatype to base (despite my usual aversion to piling more
things in there) or having a library up on Hackage just for this very
simplistic type?

(Another option would be to use `Either error ()`, which could be
lifted better into an ExceptT result, albeit with the additional
useless parameter/value in there.)

-- 
Ivan Lazar Miljenovic
Ivan.Miljenovic at gmail.com
http://IvanMiljenovic.wordpress.com


More information about the Haskell-Cafe mailing list