Skip to content

Instantly share code, notes, and snippets.

@ivanperez-keera
Last active December 2, 2015 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivanperez-keera/3587de224c9effac4391 to your computer and use it in GitHub Desktop.
Save ivanperez-keera/3587de224c9effac4391 to your computer and use it in GitHub Desktop.
Strange MultiWayIf indentation problem in Haskell
{-# LANGUAGE MultiWayIf #-}
test1 :: Maybe String -> String
test1 x =
case () of
_ | True
-> "Good morning"
test2 :: Maybe String -> String
test2 x =
if | True -> "Good morning"
| False -> "Nope"
test3 :: Maybe String -> String
test3 x =
if | Just n <- x -> "Hi"
| otherwise -> "Nope"
-- The following fails to parse because the -> are aligned with the |
test4 :: Maybe String -> String
test4 x =
if | Just n <- x
-> "Hi"
| otherwise
-> "Nope"
test5 :: Maybe String -> String
test5 x =
if | Just n <- x
, length n > 5 -- Works only if line is more indented than previous line
-> "Hi"
| otherwise
-> "Nope"
@nybble41
Copy link

nybble41 commented Dec 2, 2015

It may be unintuitive, but I don't think this is a bug. The threshold for the required indentation is set the next token following "of" for the case expression, or following "if" for the MultiWayIf. In the former case this is the pattern, and in the latter case this is the vertical bar. If you aligned the "->" with the beginning of the pattern (the underscore) in the case expression you'd get the same error. The continuation line has to be more indented than the token that began the block.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment