Skip to content

Commit dcb4f22

Browse files
eksperimentalJosé Valim
authored andcommitted
Update usage of Unicode characters in atoms, variables and friends (#8603)
Update the Syntax Reference as well as List.to_atom/1 and List.to_existing_atom/1
1 parent 6ba4cf0 commit dcb4f22

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

lib/elixir/lib/list.ex

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -748,15 +748,18 @@ defmodule List do
748748
@doc """
749749
Converts a charlist to an atom.
750750
751-
Currently Elixir does not support conversions from charlists
752-
which contains Unicode codepoints greater than 0xFF.
751+
Elixir supports conversions from charlists which contains any Unicode
752+
codepoint.
753753
754754
Inlined by the compiler.
755755
756756
## Examples
757757
758-
iex> List.to_atom('elixir')
759-
:elixir
758+
iex> List.to_atom('Elixir')
759+
:Elixir
760+
761+
iex> List.to_atom('🌢 Elixir')
762+
:"🌢 Elixir"
760763
761764
"""
762765
@spec to_atom(charlist) :: atom
@@ -768,8 +771,8 @@ defmodule List do
768771
Converts a charlist to an existing atom. Raises an `ArgumentError`
769772
if the atom does not exist.
770773
771-
Currently Elixir does not support conversions from charlists
772-
which contains Unicode codepoints greater than 0xFF.
774+
Elixir supports conversions from charlists which contains any Unicode
775+
codepoint.
773776
774777
Inlined by the compiler.
775778
@@ -779,6 +782,10 @@ defmodule List do
779782
iex> List.to_existing_atom('my_atom')
780783
:my_atom
781784
785+
iex> _ = :"🌢 Elixir"
786+
iex> List.to_existing_atom('🌢 Elixir')
787+
:"🌢 Elixir"
788+
782789
iex> List.to_existing_atom('this_atom_will_never_exist')
783790
** (ArgumentError) argument error
784791

lib/elixir/pages/Syntax Reference.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ Integers (`1234`) and floats (`123.4`) in Elixir are represented as a sequence o
2121

2222
### Atoms
2323

24-
Atoms in Elixir start with a colon (`:`) which must be followed by a non-combining Unicode character or underscore. The atom may continue using a sequence of Unicode characters, including letters, numbers, underscore, and `@`. Atoms may end in `!` or `?`. See [Unicode Syntax](unicode-syntax.html) for a formal specification.
24+
Unquoted atoms start with a colon (`:`) which must be inmediately followed by an underscore or a Unicode letter. The atom may continue using a sequence of Unicode letters, numbers, underscores, and `@`. Atoms may end in `!` or `?`. See [Unicode Syntax](unicode-syntax.html) for a formal specification. Valid unquoted atoms are: `:ok`, `:ISO8601`, and `:integer?`.
2525

26-
All operators in Elixir are also valid atoms. Valid examples are `:foo`, `:FOO`, `:foo_42`, `:foo@bar` and `:++`. Invalid examples are `:@foo` (`@` is not allowed at start), `:123` (numbers are not allowed at start) and `:(*)` (not a valid operator).
26+
If the colon is immediately followed by a pair of double- or single-quotes surrounding the atom name, the atom is considered quoted. In contrast with an unquoted atom, this one can be made of any Unicode character (not only letters), such as `:'🌢 Elixir'`, `:"++olá++"`, and `:"123"`.
2727

28-
If the colon is followed by a double- or single-quote, the atom can be made of any character, such as `:"++olá++"`.
28+
Quoted and unquoted atoms with the same name are considered equivalent, so `:atom`, `:"atom"`, and `:'atom'` represent the same atom. The only catch is that the compiler will warn when quotes are used in atoms that do not need to be quoted.
29+
30+
All operators in Elixir are also valid atoms. Valid examples are `:foo`, `:FOO`, `:foo_42`, `:foo@bar`, and `:++`. Invalid examples are `:@foo` (`@` is not allowed at start), `:123` (numbers are not allowed at start), and `:(*)` (not a valid operator).
2931

3032
`true`, `false`, and `nil` are reserved words that are represented by the atoms `:true`, `:false` and `:nil` respectively.
3133

@@ -80,13 +82,13 @@ Structs built on the map syntax by passing the struct name between `%` and `{`.
8082

8183
### Variables
8284

83-
Variables in Elixir must start with underscore or a non-combining Unicode character that is not in uppercase or titlecase. The variable may continue using a sequence of Unicode characters, including numbers and underscore. Variables may end in `?` or `!`. See [Unicode Syntax](unicode-syntax.html) for a formal specification.
85+
Variables in Elixir must start with an underscore or a Unicode letter that is not in uppercase or titlecase. The variable may continue using a sequence of Unicode letters, numbers, and underscores. Variables may end in `?` or `!`. See [Unicode Syntax](unicode-syntax.html) for a formal specification.
8486

8587
[Elixir's naming conventions](naming-conventions.html) recommend variables to be in `snake_case` format.
8688

8789
### Non-qualified calls (local calls)
8890

89-
Non-qualified calls, such as `add(1, 2)`, must start with underscore or a non-combining Unicode character that is not in uppercase or titlecase. The call may continue using a sequence of Unicode characters, including numbers and underscore. Calls may end in `?` or `!`. See [Unicode Syntax](unicode-syntax.html) for a formal specification.
91+
Non-qualified calls, such as `add(1, 2)`, must start with an underscore or a Unicode letter that is not in uppercase or titlecase. The call may continue using a sequence of Unicode letters, numbers, and underscore. Calls may end in `?` or `!`. See [Unicode Syntax](unicode-syntax.html) for a formal specification.
9092

9193
Parentheses for non-qualified calls are optional, except for zero-arity calls, which would then be ambiguous with variables. If parentheses are used, they must immediately follow the function name *without spaces*. For example, `add (1, 2)` is a syntax error, since `(1, 2)` is treated as an invalid block which is attempted to be given as a single argument to `add`.
9294

@@ -98,7 +100,7 @@ As many programming languages, Elixir also support operators as non-qualified ca
98100

99101
### Qualified calls (remote calls)
100102

101-
Qualified calls, such as `Math.add(1, 2)`, must start with underscore or a non-combining Unicode character that is not in uppercase or titlecase. The call may continue using a sequence of Unicode characters, including numbers and underscore. Calls may end in `?` or `!`. See [Unicode Syntax](unicode-syntax.html) for a formal specification.
103+
Qualified calls, such as `Math.add(1, 2)`, must start with an underscore or a Unicode letter that is not in uppercase or titlecase. The call may continue using a sequence of Unicode letters, numbers, and underscores. Calls may end in `?` or `!`. See [Unicode Syntax](unicode-syntax.html) for a formal specification.
102104

103105
[Elixir's naming conventions](naming-conventions.html) recommend calls to be in `snake_case` format.
104106

0 commit comments

Comments
 (0)