5

I just discovered that this

foo = case ((), ()) of
       ( ()
       , () ) -> ()

fails with

/tmp/wtmpf-file11080.hs:3:8:
    parse error (possibly incorrect indentation or mismatched brackets)

This can be made to work by indenting the second line of the pattern

foo = case ((), ()) of
       ( ()
        , () ) -> ()

but this feels inconsistent with my usual style, especially in

bar = case ( some lengthy :: Complicated typed expression
           , another also lengthy :: Expression with (Other types) ) of
       ( Complicated (Pattern match) to (unwrap)
       , Expression that's (Again not so short) ) -> the Rest of my Code

How should the above be rewritten / formatted to look most consistent?

leftaroundabout
  • 117,950
  • 5
  • 174
  • 319
  • If you just wanna minimise the line noise from a complex pattern you could wrap it up in a pattern synonym, or put the `case` expression in a higher-order function a la `maybe`/`either`/`foldr` – Benjamin Hodgson Dec 02 '16 at 14:31

2 Answers2

7

By the indentation rules, the code

foo = case ((), ()) of
       ( ()
       , () ) -> ()

is desugared to

foo = case ((), ()) of
       { ( ()
       ; , () ) -> ()
       }

which is a case with two branches, the first one being a syntax error.

I would recommend the following style instead:

foo = case ((), ()) of
       (( ()
        , () )) -> ()

or even (not extremely elegant, though)

foo = case ((), ()) of
       _@( ()
         , () ) -> ()
Community
  • 1
  • 1
chi
  • 111,837
  • 3
  • 133
  • 218
1

You could also just rewrite the pattern match as

   (Complicated (Pattern match) to (unwrap),
    Expression that's (Again not so short)) -> the Rest of my Code

I know it's incompatible with your style, but I think there is no real reason to use that style for tuples (aside from consistency).

Reid Barton
  • 14,951
  • 3
  • 39
  • 49