Question

In Erlang I can use define macro or .hrl file to keep the configuration at one place. Whats the best place to do it in Elixir.

I couldn't find any elegant way of doing it. Right Now I am doing something like:-

def get_server_name do
    "TEST"
end

Am I missing something?

Was it helpful?

Solution

Whether you use functions or macros should matter too much in the end, but if what you're looking for is the "keeping it in one place" part, I'd suggest putting it in its own namespace/module

defmodule MyApp.Configuration
  def server_name do
    "foo"
  end

  # or if you prefer having it all on one line
  def host_name, do: "example.com"

  # for complete equivalency, you can use a macro
  defmacro other_config do
    "some value"
  end
end

and then in your app, instead of including the file, you can alias the module so have a short prefix to indicate it's configuration, and indicate they come from elsewhere

defmodule MyApp.Server do
  alias MyApp.Configuration, as: C
end

or if you want to use the names directly

defmodule MyApp.Server do
  import MyApp.Configuration
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top