Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SyntaxError when defining module attribute with numeric-starting key #10755

Closed
vinibrsl opened this issue Feb 26, 2021 · 4 comments
Closed

SyntaxError when defining module attribute with numeric-starting key #10755

vinibrsl opened this issue Feb 26, 2021 · 4 comments

Comments

@vinibrsl
Copy link
Contributor

vinibrsl commented Feb 26, 2021

Environment

  • Elixir & Erlang/OTP versions (elixir --version): Elixir 1.11.3 (compiled with Erlang/OTP 23)
  • Operating system: macOS 11.2.2

Current behavior

When declaring module attributes starting with numbers, a syntax error pops up.

defmodule Test do
  @5_days_in_seconds 5 * 24 * 60 * 60
end
** (SyntaxError) syntax error before: '_days_in_seconds'

Expected behavior

  1. Is there any reasons not to accept numbers to start module attribute keys?
  2. If there is, should this syntax error message be more detailed?
@wojtekmach
Copy link
Member

My guess is module variables follow the same naming rules as regular variables:

iex(1)> 5_days_in_seconds = 5 * 24 * 60 * 60
** (SyntaxError) iex:1:2: syntax error before: '_days_in_seconds'

@vinibrsl vinibrsl changed the title SyntaxError when defining module attribute with number key SyntaxError when defining module attribute with numeric-starting key Feb 26, 2021
@josevalim
Copy link
Member

Correct @wojtekmach! In fact, @ is a unary operator like plus, not a special token, so you could actually write this @ foo and it would work:

iex(1)> quote do: @foo
{:@, [context: Elixir, import: Kernel], [{:foo, [context: Elixir], Elixir}]}
iex(2)> quote do: @ foo
{:@, [context: Elixir, import: Kernel], [{:foo, [context: Elixir], Elixir}]}

For now, I think we can detect the case of identifiers starting with numbers in the tokenizer and raise accordingly.

@Eiji7
Copy link
Contributor

Eiji7 commented Feb 27, 2021

Is there any reasons not to accept numbers to start module attribute keys?

@vnbrs As mentioned above module attribute have exactly same syntax as variables:

variable naming

Source: https://github.com/itsgreggreg/elixir_quick_reference#variables

I can only guess why it's like that, but I would say that's for support syntax like: 0b1 (in this case it's an integer in a binary representation).

btw. Those examples are also worth to investigate:

iex(1)> 0b2
** (SyntaxError) iex:1:2: syntax error before: b2

iex(1)> 0o9
** (SyntaxError) iex:1:2: syntax error before: o9

iex(1)> 0xG
** (SyntaxError) iex:1:2: syntax error before: xG

@kipcole9
Copy link
Contributor

Elixir is Unicode-centric and part of the decision @josevalim made is to follow the identifier syntax which is described in Unicode technical report 31 (with some incredibly minor differences). The core team also makes backward compatibility a commitment so your proposal is unlikely to get traction. If you feel passionately that the change has merit, then the recommended approach is to gather consensus in Elixir Forum or contribute a proposal to the Elixir Core mailing list.

@josevalim josevalim added this to the v1.12.0 milestone Mar 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

5 participants