檢視原始碼 Atom (Elixir v1.16.2)
Atom 是常數,其值為其本身的名稱。
它們通常用於列舉不同的值,例如
iex> :apple
:apple
iex> :orange
:orange
iex> :watermelon
:watermelon
如果 Atom 的名稱相同,則它們相等。
iex> :apple == :apple
true
iex> :apple == :orange
false
它們通常用於表達操作的狀態,方法是使用 :ok
和 :error
等值。
布林值 true
和 false
也是 Atom
iex> true == :true
true
iex> is_atom(false)
true
iex> is_boolean(:false)
true
Elixir 允許您跳過 Atom false
、true
和 nil
的前導 :
。
Atom 必須由 Unicode 字元組成,例如字母、數字、底線和 @
。如果關鍵字具有不屬於上述類別的字元,例如空格,您可以用引號將其包起來
iex> :"this is an atom with spaces"
:"this is an atom with spaces"