-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
atom.ex
82 lines (60 loc) · 1.67 KB
/
atom.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
defmodule Atom do
@moduledoc """
Atoms are constants whose values are their own name.
They are often useful to enumerate over distinct values, such as:
iex> :apple
:apple
iex> :orange
:orange
iex> :watermelon
:watermelon
Atoms are equal if their names are equal.
iex> :apple == :apple
true
iex> :apple == :orange
false
Often they are used to express the state of an operation, by using
values such as `:ok` and `:error`.
The booleans `true` and `false` are also atoms:
iex> true == :true
true
iex> is_atom(false)
true
iex> is_boolean(:false)
true
Elixir allows you to skip the leading `:` for the atoms `false`, `true`,
and `nil`.
Atoms must be composed of Unicode characters such as letters, numbers,
underscore, and `@`. If the keyword has a character that does not
belong to the category above, such as spaces, you can wrap it in
quotes:
iex> :"this is an atom with spaces"
:"this is an atom with spaces"
"""
@doc """
Converts an atom to a string.
Inlined by the compiler.
## Examples
iex> Atom.to_string(:foo)
"foo"
"""
@spec to_string(atom) :: String.t()
def to_string(atom) do
:erlang.atom_to_binary(atom)
end
@doc """
Converts an atom to a charlist.
Inlined by the compiler.
## Examples
iex> Atom.to_charlist(:"An atom")
'An atom'
"""
@spec to_charlist(atom) :: charlist
def to_charlist(atom) do
:erlang.atom_to_list(atom)
end
@doc false
@deprecated "Use Atom.to_charlist/1 instead"
@spec to_char_list(atom) :: charlist
def to_char_list(atom), do: Atom.to_charlist(atom)
end