官术网_书友最值得收藏!

Atoms

Atoms are a constant, whose value is its own name. They are always prefixed with a leading colon (:), followed by alphanumeric characters (and possibly _ or @). They may terminate with an exclamation or a question mark. Atoms are similar to enumerations in C and symbols in Ruby. Here are some examples of atoms:

iex> :ok
:ok
iex> :error
:error
iex> :some_descriptive_name!
:some_descriptive_name!
iex> :value@start
:value@start

You can create atoms with arbitrary characters with the following syntax:

iex> :"Atom name with arbitrary characters#$%^"
:"Atom name with arbitrary characters#$%^"

As with all data structures in Elixir, atoms can't be modified after their creation. Furthermore, they are not garbage-collected. Atoms are kept in the atom table, and upon compilation, their value is replaced by a reference to their entry on this table. This makes comparing atoms very efficient. As you'll learn throughout this book, this is one of the major use cases for atoms in Elixir, as we are constantly matching the return of a function against a certain expected atom.

Since atoms are not garbage collected, don't create atoms dynamically from sources you can't control, as you can very easily use up all of the space allocated for the atom table. For instance, if you're parsing a JSON response and creating a map out of it, don't use atoms for its keys—use strings instead (both of these types, maps and strings, will be described later in this chapter).
主站蜘蛛池模板: 宁城县| 喀喇沁旗| 阿坝县| 本溪| 张掖市| 罗江县| 松滋市| 徐汇区| 兴和县| 潼关县| 武平县| 高尔夫| 那曲县| 永修县| 呼图壁县| 英德市| 翼城县| 红安县| 卢龙县| 桓台县| 丘北县| 开原市| 林州市| 勐海县| 天祝| 濮阳县| 阿拉善右旗| 富裕县| 电白县| 稻城县| 六盘水市| 井陉县| 临汾市| 涞源县| 温宿县| 绵竹市| 博罗县| 荆州市| 拜泉县| 阜康市| 宁强县|