unisonweb / unison

A friendly programming language from the future
https://unison-lang.org
Other
5.81k stars 272 forks source link

Underscores in numeric literals #2228

Open philderbeast opened 3 years ago

philderbeast commented 3 years ago

Please allow for underscores in numeric literals.

# scratch.u
x : Float
x = 0.000_000_000_001

# ucm
This looks like a function call, but with a Float where the function should be.  Are you missing an operator?

      2 | x = 0.000_000_000_001
# scratch.u
unique type GeodeticAccuracy = GeodeticAccuracy Float

defaultGeodeticAccuracy : GeodeticAccuracy
defaultGeodeticAccuracy = GeodeticAccuracy 0.000_000_000_001

# ucm
This looks like a function call, but with a GeodeticAccuracy where the function should be.  Are you missing an operator?

      4 | defaultGeodeticAccuracy = GeodeticAccuracy 0.000_000_000_001
philderbeast commented 3 years ago

The kowainik/tomland project also uses megaparsec and accepts underscores in numeric literals with this pull request by the use of sepBy1 (some parseDigit) (char '_'). The relevant parser in unison is:

https://github.com/unisonweb/unison/blob/c7b67bd4607665ed5168e350782b67445271aac6/parser-typechecker/src/Unison/Lexer.hs#L732-L749

pchiusano commented 3 years ago

Sounds like a good idea! Want to do a PR?

runarorama commented 3 years ago

One issue with this is that the underscores would be lost as soon as they enter the codebase, unless the pretty-printer always prints them as well. But that's not a clear win for Unison, as it makes the output of e.g. watch expressions sometimes annoying to use.