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

AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31707 143
New
AstonJ
If a newbie asked you about Phoenix Contexts, how would you explain the basics to them? Feel free to be as concise or in-depth as you li...
New
chuck
Let me start by stating an assumption: Phoenix is a great approach to building REST APIs. There are many reasons for this, but I will ass...
New
Fl4m3Ph03n1x
Background A few days ago I was listening to The future of Elixir from Elixir Talks, with Dave Thomas (@pragdave ) and Brian Mitchell. I...
New
mikl
I wanted to capitalize a string, and tried using String.capitalize(). That generally works well, until you try to capitalize a word like...
New
opsb
We’re considering our architecture from a viewpoint of scaling our traffic heavily over the next 6 months. Our current deployment is runn...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement