3 ms·
The real trick is that, conceptually, these are not really "unwrapping" functions--instead, they're "propagating" functions. All they do is take Maybe values fr
by tikhonj 14y ago
The real trick is that, conceptually, these are not really "unwrapping" functions--instead, they're "propagating" functions. All they do is take Maybe values from deep inside your computation and string them through to the outside.
In practice, this means that you write a decent part of your program using these techniques, which creates a block of code that produces a Maybe value after taking a bunch of Maybe inputs. Then you only use a case statement at the very end, when you need to plug the Maybe value back into normal code.
All these functions are useful for one particular case: you don't know what to do with a Nothing value, so if you see one anywhere, you just pass it on: your final result is Nothing. That pattern just turns out to be very useful.
- aaronblohowiak 14y ago>That pattern just turns out to be very useful. Like NaN, it can be hard to track down where things went wrong.
- Evbn 14y agoIf Nothing is ever an error, than you can add code to handle that case. It isn't really different from some method returning an empty list and other functions being basically no-ops afterward.
- dllthomas 14y agoBut that's precisely why you abstract it; so you can switch to Error in need be...
- masklinn 14y agoExcept contrary to NaN, the type system encodes where it can exist and what its span is.
- dllthomas 14y agoRight, you can rule out the pieces that are typed as "NaN-free" - hopefully that's a lot. This is a great reason to avoid huge chunks of code stitched together staying inside Maybe, while still being convenient on the small scale.
- Tyr42 14y agoSo, Either String a <=> Maybe a, but with a nice reason something went wrong.
- mercurial 14y agoNot quite. They are not restricted to error handling. You can have methods returning a Maybe something without it being an error (just like there are plenty of valid reasons for returning null instead of throwing an exception). You can also use Maybe in a data structure: data Employee = Employee { name : Text, spouse : Maybe Text } You may also want to use Either to store two possible outcomes of an operation, though I would recommend using your own sum type for clarity: data MyOwnEither a b = MyLeft a | MyRight b
- mokus 14y ago> Like NaN, it can be hard to track down where things went wrong. Unlike NaN, you have the freedom to not use it when you don't want it.