Why I cannot use tuple as application env key?

borja.deregil borja.deregil@REDACTED
Thu Oct 29 14:39:01 CET 2020


> I want to use tuple as application env key:
> 
> application:set_env(sessions, {backend,Name},  Configuration).
> 
> 
> Why is it forbidden? Why application_controller assumes that I must
> use only atoms?

If you use application:set_env/1 or application:set_env/2, you're right 
that
application_controller will check that your key is an atom.
If you use application:set_env/3, you can use any term as key (I just 
tried locally).

See 
https://github.com/erlang/otp/blob/0f0100618e3d050ea9f00517a4d7ba00bfb740b9/lib/kernel/src/application_controller.erl#L473-L474 
and 
https://github.com/erlang/otp/blob/0f0100618e3d050ea9f00517a4d7ba00bfb740b9/lib/kernel/src/application_controller.erl#L871-L872

> You can try to use persistent option and will see that it doesn't work.

Can you share what doesn't work? I can recommed the approach shared by 
bengt, use
application:get_env/2 at the beggining of your application, and use 
persistent_term or and
ETS table to store configuration in the format that you want. Both ETS 
and persistent_term
allow any Erlang term as the key.

Cheers,
Borja


More information about the erlang-questions mailing list