dogweather

dogweather

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,

First Post!

LostKobrakai

LostKobrakai

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.

Most Liked

r8code

r8code

maybe this help

Last Post!

dogweather

dogweather

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

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

Where Next?

Popular in Discussions Top

Crowdhailer
I’ve been hearing much about the new formatter and it’s something I have been keen to try. I find examples buy far the most illuminating...
248 19760 150
New
pillaiindu
In django there is a cache framework backed by memcached. Rails also puts a lot of emphasis on caching, and even the idea of russian-doll...
New
New
thojanssens1
It would be nice to be able to define a redirect from one route to another from the router.ex file. E.g.: redirect "/", UserController, ...
New
Fl4m3Ph03n1x
Background This question comes mainly from my ignorance. Today is Black Friday, one of my favorite days of the year to buy books. One boo...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
Rustixir
Hi everyone, im working on find best language/framework/system for high concurrency, high performance and stable performance after wor...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44608 311
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New