mmyers

mmyers

Why are Application.{fetch,get,compile}_env functions not consistent?

In the Application module, I know compile_env is newer than get_env and fetch_env, but it seems like it would be helpful to have the same function signature that allows passing a “key_or_path” to all of these.

compile_env(app, key_or_path, default \\ nil)
get_env(app, key, default \\ nil)
fetch_env(app, key)

I’d like the ability to do this:
path = Application.fetch_env!(:device, [:connection, :polling_interval])
and have the run time checks related to compile_env catch it.

Here is why I’m thinking of this.
Note: I realize this is contrived, and you’d almost have to try to do it wrong to have this code. but still, why shouldn’t we make the functions have the same signature?

When I have a config, sometimes I want to make parts of it set at compile time, and other parts of the config dynamic (set at run time).

And without matching calls, it seems like you could hide the problem compile_env is trying to fix.
For example:

If you have this in one place:
@polling_interval Application.compile_env!(:backend, [:connection, :polling_interval])

Nothing prevents you from doing this in another place:

# In a config file like config/releases.exs 
custom_polling_interval = System.get_env("POLLING_INTERVAL")

config :backend, :connection,
  polling_interval: custom_polling_interval,

And then trying to do this (and getting a different value than the result from compile_env), which is what compile_env tries to help us avoid.

connection = Application.get_env(:device, :connection)
path = Keyword.fetch(connection, :polling_interval)

Maybe that will still get a runtime error, and my example is invalid. :man_shrugging:

First Post! Switch mode

tcoopman

tcoopman

Bumping this topic because I just noticed that compile_env takes a path and I was also confused why the others don’t allow a path.

Most Liked

LostKobrakai

LostKobrakai

I can’t find the source anymore, but iirc it’s plain backwards compatibility. Application.get_env(:myapp, [:abc, :def]) means it’s looking for the key [:abc, :def], not the value at the path [:abc, :def].

Last Post!

tcoopman

tcoopman

Hadn’t thought about that. That would be a valid reason

Where Next?

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2976 91332 914
New
byu
@chrismccord : I just saw the Extract AGENTS.md from Phoenix.new into phx.new generator commit to the phoenix project. My initial shotgu...
New
arcanemachine
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
AstonJ
Just a general thread to post chat/news/info relating to AI/ML stuff that may be relevant for Nx now or in the future. Got anything to sh...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
zachdaniel
Introducing AshStorage! Attachment and file management that slots directly into your resources :smiling_face_with_sunglasses: I had hope...
New

We're in Beta

About us Mission Statement