I am aware of several possibilities, but all of them implicitly include code for throwing errors in the case of Nothing .
Unusual pattern:
λ> let Just a = Just "hi" λ> a "hi" λ> let Just a = Nothing λ> a *** Exception: <interactive>:4:5-20: Irrefutable pattern failed for pattern Data.Maybe.Just a
Non-flowing patterns:
λ> let f (Just a) = a λ> f (Just "hi") "hi" λ> f Nothing *** Exception: <interactive>:6:5-18: Non-exhaustive patterns in function f
fromJust (check and throw error):
λ> fromJust (Just "hi") "hi" λ> fromJust Nothing *** Exception: Maybe.fromJust: Nothing
You cannot use unsafeCoerce , because for all a internal representations of Maybe a and a different, and I don’t know how to tell GHC not to check other cases in an irrefutable picture.
Have you shown that this behavior is undesirable performance and that there are no other, easier optimizations? If you do not, I would not worry about that :)
source share