Rules for identifier names

437 views
Skip to first unread message

Elias Dorneles

unread,
May 14, 2016, 2:37:31 PM5/14/16
to Elm Discuss
Hello!

Just another Elm begginer here, having fun with it. :)

I'm trying to understand what are the exact rules for identifier names (functions, variables, modules, etc), and I didn't find it in the documentation.
I'd expect it to be at: http://elm-lang.org/docs/syntax
Are the rules documented somewhere else?

For those interested in the perspective of beginners in the language: when I use an invalid identifier name (with an upper-case first letter), I get an error message that points me to whitespace (see below). I imagine others must've bumped into similar issues, perhaps this could be improved?

Thank you!


$ elm repl
---- elm repl 0.17.0 -----------------------------------------------------------
 :help for help, :exit to exit, more at <https://github.com/elm-lang/elm-repl>
--------------------------------------------------------------------------------
> Something = 1
-- SYNTAX PROBLEM -------------------------------------------- repl-temp-000.elm

I need whitespace, but got stuck on what looks like a new declaration. You are
either missing some stuff in the declaration above or just need to add some
spaces here:

4| t_s_o_l = ()
   ^
I am looking for one of the following things:

    whitespace


> something? = 234
-- SYNTAX PROBLEM -------------------------------------------- repl-temp-000.elm

I ran into something unexpected when parsing your code!

4|   something? = 234
                ^
I am looking for one of the following things:

    an expression
    whitespace



Joey Eremondi

unread,
May 14, 2016, 2:52:28 PM5/14/16
to elm-d...@googlegroups.com
Think "more permissive than C" but "less permissive than scheme.

1. Functions are values, so they are in the same namespace, and the parser doesn't see them differently at all.
2. Functions and values must start with a lowercase letter.
3. Types, Type Constructors, and Value Constructors must start with an uppercase letter.
4. Underscores are ok, dashes are not, since it's ambiguous whether x-y is subtraction or a name. That said, CamelCase is standard.
5. You can append a "prime" single quote ' at the end of names, so x' = 3 is totally valid.

On elm-lang.org/try, "t_s_o_l" works, but anything with "?" doesn't. I think it might be a reserved character as an operator, like +, -, *, etc.

Lots of effort has been put into type errors, but I suspect much less with parser errors, so logging poor messages in this case is good.

Unfortunately, there's not a great singular reference on the parser, but if you're really curious you want, you can see its code here: https://github.com/elm-lang/elm-compiler/tree/master/src/Parse

The best practice is just to follow examples and look at what standard code does.


--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nick H

unread,
May 14, 2016, 2:53:02 PM5/14/16
to elm-d...@googlegroups.com
I have never seen this documented anywhere, but I am pretty confident that the naming rules are the same as in Haskell (see Names and Operators).

- Valid name characters are lower and upper case letters, numbers, the apostrophe ( ' ) and the underscore ( _ ).

- If the thing being named is a module, type, or type constructor, the first character must be a capital letter. For everything else, it must be a lowercase letter or an underscore.

- Operator names can use only these characters: !#$%&*+./<=>?@\^|-~

(Style note: Elm favors camelCase over snake_case. It is also preferable to stick to alphanumeric characters for names, and to avoid apostrophes, underscores and operators.)

On Sat, May 14, 2016 at 11:37 AM, Elias Dorneles <eliasd...@gmail.com> wrote:

Elias Dorneles

unread,
May 14, 2016, 3:06:14 PM5/14/16
to Elm Discuss
Thank you, fellows, this is helpful.

> On elm-lang.org/try, "t_s_o_l" works, but anything with "?" doesn't. I think it might be a reserved character as an operator, like +, -, *, etc.

Interesting that you guessed I had tried using "t_s_o_l", but the expression I had actually typed was: Something = 1
The t_s_o_l showed up in the error message, don't know why.

I suppose this tells us that message really needs improvements. :)

I've just found out there is already a bug report about it: https://github.com/elm-lang/elm-repl/issues/85

So, nevermind, and sorry for the noise.
Thanks

Joey Eremondi

unread,
May 14, 2016, 3:14:16 PM5/14/16
to elm-d...@googlegroups.com
Valid name characters are lower and upper case letters, numbers, the apostrophe ( ' ) and the underscore ( _ ).

Unlike Haskell, I don't think you can start an identifier with an Underscore in Elm. (Possible a bug?)


So, nevermind, and sorry for the noise.

This is not noise! We need contributions like this to help improve Elm and its documentation. Beginner friendlyness (and non-dependence on Haskell) is a goal of Elm!
 

--

Nick H

unread,
May 14, 2016, 3:18:48 PM5/14/16
to elm-d...@googlegroups.com
Oops, looks like you're right, Joey. I think it would only be a bug if the naming conventions were actually documented somewhere :-)
Reply all
Reply to author
Forward
0 new messages