Compiler: Negative numbers in pattern matches are syntax errors

Created on 14 Jan 2016  ·  8Comments  ·  Source: elm/compiler

This is an sscce version of #1259

f x =
  case x of
    1 -> "ok"
    -1 -> "breaks parsing"
    _ -> "ok"

Gives a syntax error.

If you swap the -1 pattern with the previous line, everything compiles, so the problem seems to affect only patterns after the first one.

bug parsing

Most helpful comment

An update for 0.19: the workaround at https://github.com/elm/compiler/issues/1261#issuecomment-172385182 no longer works. As far as I can tell, negative numbers aren't allowed in pattern matches as either the first or non-first pattern, and the presence/absence of parentheses does not make them allowed either.

All 8 comments

Workaround: wrap the -1 in parens: (-1).

Thanks for the SSCCE. Follow along in #1374

Okay, making this it's own thing again because all the other things in the meta issue are done.

For my future reference: I think it should only be possible to match on Int and I'm not sure if there are any languages that permit negative numbers in matches.

Haskell allows it if it's parenthesized:

\(-3) -> 1 works on tryhaskell.org, but \-3 -> 1 gives a parse error.

OCaml works with or without parenthesis.

F# (and Fable) and Scala (Scala.js) allow negative numbers in patterns as well.

An update for 0.19: the workaround at https://github.com/elm/compiler/issues/1261#issuecomment-172385182 no longer works. As far as I can tell, negative numbers aren't allowed in pattern matches as either the first or non-first pattern, and the presence/absence of parentheses does not make them allowed either.

@aecay See also #1773

Was this page helpful?
0 / 5 - 0 ratings