haskell-issues icon indicating copy to clipboard operation
haskell-issues copied to clipboard

Language extension: caseMay

Open Gurkenglas opened this issue 8 years ago • 3 comments

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.

Gurkenglas avatar Apr 02 '16 08:04 Gurkenglas

You can use the fmap function for that. It will only operate on the Just values

fmap :: (a -> b) -> Maybe a -> Maybe b

nikivazou avatar Feb 07 '17 17:02 nikivazou

@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'

neongreen avatar Feb 07 '17 17:02 neongreen

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.

nikivazou avatar Feb 07 '17 18:02 nikivazou