Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it a syntax error to linebreak case matches without extra indentation, and what's the recommended style to get around it?

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?

like image 522
leftaroundabout Avatar asked Dec 02 '16 14:12

leftaroundabout


2 Answers

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
       _@( ()
         , () ) -> ()
like image 60
chi Avatar answered Nov 15 '22 12:11

chi


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).

like image 40
Reid Barton Avatar answered Nov 15 '22 12:11

Reid Barton