1

I can't find where I saw this but I found somewhere on a reddit discussion the following syntax in a do block:

do
  case (<- fooM) of
    Foo x -> ..
    ...

instead of the usual

do
  foo <- fooM
  case foo of
    Foo x -> ..
    ...

Which would be extremely useful but it doesn't seem to work (without a language extension) and I was wondering if it was just shorthand of the post author or if I am missing a language extension.

Note that I understand that this syntax is ambiguous for example

(`execState` 0) $ do
  return $ (<- put 3) <> (<- put 4) <> (<- put 5)

We may get 3, 4 or 5 but there could be semantics to mitigate this.

fakedrake
  • 6,528
  • 8
  • 41
  • 64

2 Answers2

11

You can use LambdaCase here!

{-# LANGUAGE LambdaCase #-} 

doStuff = do
  fooM >>= \case
    Foo x -> ...
sara
  • 3,521
  • 14
  • 34
3

I don't think any such thing exists; if it did, I would only expect it to work for case, and not inside of arbitrary expressions as your second example does.

I looked through the list of GHC language extensions and none of them look promising.

amalloy
  • 89,153
  • 8
  • 140
  • 205
  • 2
    @fakedrake: Did you notice the other answer given 30 minutes before you wrote this comment? – sshine Mar 05 '19 at 14:40
  • I did but it does not address my final note which is very important. The correct answer seems to be "there is no such thing" so I accepted this and upvoted the other one. – fakedrake Mar 12 '19 at 13:57