I’m just wondering if anyone ever has any clever use-cases for atoms named starting with a colon followed by a captial: :Atom
. Is there any kind of trending convention around it? I’ve been using it to give a “private” name to a GenServer for sending it debug messages in iex. Is there a better way of doing that? Is there a reason to never use it? This is more of just a fun question—or tedious, depnding on your viewpoint
Module names in Elixir are atoms starting with capital letters. When you especially want to refer to a module in code, you use module names typically without colons.
I sometimes use them to choose a module implementing the same behavior, for instance in a configuration file.
I would find that convention confusing since I would not be sure if you meant to say Atom
(with no :
) and therefore a module name, or not.
Part of why I would be unsure of your intent is because:
iex> Atom == :Atom
false
iex> Atom == :"Elixir.Atom"
true
@smileytoemoji [ :P: 😛 ]
if you want a “private” atom, surround it by a double underscore.
:__atom__
,
that is the convention used in Elixir.
For a real case, check this PR Rename struct attribute in defstruct by eksperimental · Pull Request #10354 · elixir-lang/elixir · GitHub and the issue linked in it,
It is not about atoms but attributes, but I guess it is the same convention.
Structs use the private key named :__struct__
to store the name of the struct.
I use them because a foreign schema has uppercase identifiers:
<xs:simpleType name="Layout_t">
<xs:restriction base="xs:string">
<xs:enumeration value="Table"/>
<xs:enumeration value="Grid"/>
<xs:enumeration value="List"/>
</xs:restriction>
</xs:simpleType>
embedded_schema do
...
field(:layout, Ecto.Enum, values: [:Table, :Grid, :List], default: :List)