Idea for compiler-checked Application env

Would it be possible for Mix.Config's config to be a macro which creates accessor functions for the environment variables? E.g.:

# config/config.exs
config :my_app, :key, "value"

…currently enables this kind of access:

"value" = Application.fetch_env!(:my_app, :key)

But instead, I imagine the config macro generates functions at compile time enabling this usage:

"value" = Application.MyApp.key()

The big win is the compile-time checking & LS completion support for MyApp and key. It’s also shorter and more expressive. It doesn’t rely on the programmer remembering to use fetch_env! vs. get_env.

I’m new to macros as well as the Application + Mix.Config system. Would this be possible?

Update: A couple of existing solutions are,

1 Like

Config files are scripts (.exs) and therefore don‘t persist their compiled contents. Also the app env is not just filled by those config files. Say you switch to e.g. a runtime secrets provider started with your app. Suddenly all you modules would be missing.

maybe this help

2 Likes

Or this one: https://twitter.com/sasajuric/status/1226922505392328706?s=21

1 Like

by @sasajuric :slight_smile:

Thanks! That provides exactly what I was thinking of, e.g.:

Myapp.Config.hostname/0  # for retrieving the hostname.