Hello. I recently discovered Application/get_application/1 and now wonder why specifying the otp app with
use in libraries is so prevalent?
The only thing I can think of is that they need the otp app as compile time info, but how and why? Usually it’s simply used to fetch config with something like
Application.get_env(@otp_app, __MODULE__, ) which is a runtime operation anyway.
So fetching config becomes like this instead:
Application.get_application(__MODULE__) |> Application.gen_env(__MODULE__, )
If for some reason you want to config your module outside of the app it was defined in, you can do something like:
(@use_opts[:otp_app] || Application.get_application(__MODULE__)) |> Application.gen_env(__MODULE__, )
otp_app: :my_app from all my libraries and switching to
Application.get_application/1 to get config, everything seems fine… and it’s really nice having less things to configure (especially in a large umbrella app where modules get moved around to different apps frequently), but I’m wondering if there are any hidden dragons to this (and if not, why this pattern is so prevalent in libraries).
Thanks for the info!