elm / compiler

Compiler for Elm, a functional language for reliable webapps.
https://elm-lang.org/
BSD 3-Clause "New" or "Revised" License
7.56k stars 662 forks source link

Negative numbers in pattern matches are syntax errors #1261

Open rtfeldman opened 8 years ago

rtfeldman commented 8 years ago

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.

yawaramin commented 8 years ago

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

evancz commented 8 years ago

Thanks for the SSCCE. Follow along in #1374

evancz commented 7 years ago

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.

JoeyEremondi commented 7 years ago

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.

yawaramin commented 7 years ago

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

crazymykl commented 6 years ago

Rust also allows negatives: https://play.rust-lang.org/?gist=017c59b66ce0a3b5f080dad3cf9d2999&version=stable&mode=debug&edition=2015 .

aecay commented 6 years ago

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.

lydell commented 6 years ago

@aecay See also #1773