Elixir原子

示例

原子是代表事物名称的常量。一个原子的值就是它的名字。原子名称以冒号开头。

:atom   # that's how we define an atom

原子的名称是唯一的。具有相同名称的两个原子始终相等。

iex(1)> a = :atom
:atom

iex(2)> b = :atom
:atom

iex(3)> a == b
true

iex(4)> a === b
true

布尔值true和false实际上是原子。

iex(1)> true == :true
true

iex(2)> true === :true
true

原子存储在特殊原子表中。知道此表不是垃圾收集,这一点非常重要。因此,如果您想要(或偶然地是事实)不断创建原子,则是个坏主意。