elm / error-message-catalog

A catalog of broken Elm programs / data to improve error messages
BSD 3-Clause "New" or "Revised" License
173 stars 17 forks source link

Trying to create record key with double quotes #220

Closed hsribei closed 5 years ago

hsribei commented 7 years ago

When following the "Core Language" section in the official guide, I thought to try if record keys could be any object. The error messages missed the point:

> bill = { "name" = "Gates", age = 57 }
-- SYNTAX PROBLEM -------------------------------------------- repl-temp-000.elm

The = operator is reserved for defining variables. Maybe you want == instead? Or
maybe you are defining a variable, but there is whitespace before it?

4|   bill = { "name" = "Gates", age = 57 }
          ^
Maybe <http://elm-lang.org/docs/syntax> can help you figure it out.

> bill = { name = "Gates", age = 57 }
{ name = "Gates", age = 57 } : { age : number, name : String }

The compiler hints could include "record key names don't accept quotes" or something to that effect.

evancz commented 5 years ago

Thank you for the report! I am finishing up an overhaul of the parser that does a bunch of work to improve parse errors. My development build in now showing:

---- Elm 0.19.1 ----------------------------------------------------------------
Say :help for help and :exit to exit! More at <https://elm-lang.org/0.19.1/repl>
--------------------------------------------------------------------------------
> bill = { "name" = "Gates", age = 57 }
|   
-- UNFINISHED RECORD ------------------------------------------------------ REPL

I just started parsing a record, but I got stuck here:

2| bill = { "name" = "Gates", age = 57 }
            ^
Records look like { x = 3, y = 4 }, so I was expecting to see a field name next.

So it is better, but it would be cool to have a specific note about double quotes in this case. I'll see how hard it'd be to add and get back to you about it!

evancz commented 5 years ago

Okay, with the tweak in https://github.com/elm/compiler/commit/fea01868937a0dce6e5e490414578c2701ea571f and https://github.com/elm/compiler/commit/98e9fb9433cba52f52cfd2770ce7bb15f80aa26a, the latest error message is like this:

---- Elm 0.19.1 ----------------------------------------------------------------
Say :help for help and :exit to exit! More at <https://elm-lang.org/0.19.1/repl>
--------------------------------------------------------------------------------
> bill = { "name" = "Gates", age = 57 }
|   
-- PROBLEM IN RECORD ------------------------------------------------------ REPL

I just started parsing a record, but I got stuck here:

2| bill = { "name" = "Gates", age = 57 }
            ^
I was expecting to see a record field defined next, so I am looking for a name
like userName or plantHeight.

Note: Field names must start with a lower-case letter. After that, you can use
any sequence of letters, numbers, and underscores.

Note: If you are trying to define a record across multiple lines, I recommend
using this format:

    { name = "Alice"
    , age = 42
    , height = 1.75
    }

Notice that each line starts with some indentation. Usually two or four spaces.
This is the stylistic convention in the Elm ecosystem.

Hopefully that'll be enough to help folks coming from JS see that the double-quote style is not possible in Elm.

Thank you for reporting this case!