Is this a correct way to avoid wasting or reaching the atom limit?

String.to_atom creates a new atom if one of its name wasn’t use before, which is dangerous for dynamic/unknown data. String.to_existing_atom does only allow you to convert strings to atoms when the atom already exists at that point via some other means of usage of it.

iex(5)> str = "some_random_not_yet_used_atom"
"some_random_not_yet_used_atom"
iex(6)> String.to_existing_atom(str)
** (ArgumentError) argument error
    :erlang.binary_to_existing_atom("some_random_not_yet_used_atom", :utf8)
iex(6)> String.to_atom(str)
:some_random_not_yet_used_atom
iex(7)> String.to_existing_atom(str)
:some_random_not_yet_used_atom
2 Likes