Use of binary_to_existing_atom(Binary, Encoding)

erlang

Solution

It's enough that the atoms are used in any loaded module, e.g. matched in a `case` expression or some other comparison.

If the comparison happens in a different module from where you call `binary_to_existing_atom`, then you need to ensure that that module is loaded by the time you do the conversion. If you build a release and run it in "embedded" mode, then all modules will be loaded on startup. Otherwise, modules will be loaded when they are called, so if the module in question is e.g. a gen_server started by the application's supervision tree, then you're fine.

Problem

I would like to convert some binaries coming from a socket to atoms that I will use later in gen_server calls. Since I don't want to flood the VM with new (and possibly non valid atoms) I use: binary_to_existing_atom(Binary, Encoding) Now, how (or better where) should I declare the valid atoms at systems startup so that the conversion will be valid?

Original source