haskell-issues
haskell-issues copied to clipboard
Language extension: caseMay
caseMay x of
A -> a
B -> b
would be desugared to
case x of
A -> Just a
B -> Just b
_ -> Nothing
. Then there would be the corresponding LambdaCase equivalent and perhaps this would all be generalized to any Alternative. Compare fail's use in do notation.
You can use the fmap
function for that. It will only operate on the Just
values
fmap :: (a -> b) -> Maybe a -> Maybe b
@nikivazou How would it be used in this case? Specifically, here's what @Gurkenglas wants to do:
data Letter = A | B | C | D | E | ...
main = do
-- This should print "Nothing"
print $ caseMay C of
A -> 'a'
B -> 'b'
-- This should print "Just 'a'"
print $ caseMay A of
A -> 'a'
B -> 'b'
Oh sorry, I though you were matching against Maybe
values.
I see, so you are adding this _ -> Nothing
case on your match! It is not a bad idea.