How do you define constants in Elixir modules?

Martimatix picture Martimatix · Nov 22, 2015 · Viewed 18.9k times · Source

In Ruby, if one were defining constants in classes, they would define them using all caps. For example:

class MyClass
  MY_FAVORITE_NUMBER = 13
end

How do you do this in Elixir? And if no such equivalent exists, how do you get around the problem of magic numbers in Elixir?

Answer

AbM picture AbM · Nov 22, 2015

You can prepend your variable name with @:

defmodule MyModule do
  @my_favorite_number 13
end

Here are the docs