aelve / haskell-issues

An unofficial issue tracker for all things Haskell-related
18 stars 0 forks source link

Language extension: caseMay #32

Open Gurkenglas opened 8 years ago

Gurkenglas commented 8 years ago
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.

nikivazou commented 7 years ago

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

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

neongreen commented 7 years ago

@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'
nikivazou commented 7 years ago

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.