60

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?

6 Answers 6

64

You can prepend your variable name with @:

defmodule MyModule do
  @my_favorite_number 13
end

Here are the docs

8
  • 20
    but @my_favorite_number can not be accessed from outside of the module
    – lfx_cool
    Apr 21, 2016 at 7:33
  • 2
    @lfx_cool correct. if you want to access it from outside the module, you can define a function that returns its value: def my_favorite_number, do: @my_favorite_number
    – AbM
    Apr 21, 2016 at 11:56
  • 2
    it's a getter method, if we have shortcuts to do this, e.g. attr_reader :my_favorite_number
    – lfx_cool
    Apr 21, 2016 at 13:37
  • 6
    I wouldn't call it a "method" at all. I'd call it a function that returns a value. A method has the implied scope of a class; a function has no such implied scope. A method can access any class variables which are present in the class by default. A function cannot access values which are not either passed in or defined in the function. Change your thinking. Sep 26, 2016 at 18:36
  • 2
    @OnorioCatenacci, to be fair, in this case it has the implied scope of the module instead, not much better. For somebody coming from OO, it makes it more confusing because you learn FP, you're told about pure functions and then you get this example which is clearly not a pure function. It takes no argument and returns something that is based on the state of the module. I love Elixir though and I think side effects are difficult to avoid (for IO for example), but module attributes with an "@" like instance variable in Ruby makes it extra confusing.
    – Mig R
    Aug 7, 2018 at 15:46
14

Looking around github I see this being used which appeals to me. Allows the constant to be accessed from other modules.

ModuleA
  @my_constant 23

  defmacro my_constant, do: @my_constant


ModuleB
  Require ModuleA
  @my_constant ModuleA.my_constant

Yet again simple and facinating elixir solution

1
  • Why should we create a macro when we can use a simple function? Is there some specific benefit?
    – Spyros
    Aug 31, 2021 at 10:06
13

Another approach to defining constants is one that I took with the wxErlang header files. That is, you can simply define a single line function that returns the constant value for you. Like so:

  def wxHORIZONTAL, do: 4
  def wxVERTICAL, do: 8
  def wxBOTH, do: (wxHORIZONTAL ||| wxVERTICAL)

and here's another example from the same source code:

 # From "defs.h": wxDirection
  def wxLEFT, do: 16
  def wxRIGHT, do: 32
  def wxUP, do: 64
  def wxDOWN, do: 128
  def wxTOP, do: wxUP
  def wxBOTTOM, do: wxDOWN
  def wxNORTH, do: wxUP
  def wxSOUTH, do: wxDOWN
  def wxWEST, do: wxLEFT
  def wxEAST, do: wxRIGHT
  def wxALL, do: (wxUP ||| wxDOWN ||| wxRIGHT ||| wxLEFT)

As you can see it makes it a little easier to define a constant in terms of another constant. And when I want those constants in a different module all I need to do is to require WxConstants at the top of the module. This makes it much easier to define a constant in one place and use it in several others--helps a lot with DRY.

By the way, you can see the repo here if you're curious.

As I say, I add this answer mostly for sake of completeness.

3
  • 3
    That's cool, but sometimes I want a constant that's a computed value, which apparently leads down the rabbit hole of function memoization stackoverflow.com/questions/35465306/… . Useful to know about in case the need arises.
    – Al Chou
    Feb 22, 2016 at 3:03
  • I'm not sure what you mean by computed_value--what's the difference between what you mean by computed value and, for example, wxALL above? Feb 23, 2016 at 12:52
  • wxALL is an OR of constants, while what I was thinking of was a method call, say like Time.now.
    – Al Chou
    Aug 11, 2016 at 17:22
13

Maybe you define a constants module file and in it you can define macros for this like so

defmodule MyApp.Constants do
  defmacro const_a do
    quote do: "A"
  end
end

You use it by importing it into any other module

defmodule MyApp.ModuleA do
  import MyApp.Constants

  def get_const_a do
    const_a()
  end 
end

The benefit is also that you don't incur any runtime cost as well as the advantage of using it in case matches

case something do
  const_a() -> do_something
  _ -> do_something_else
end
2
  • This is honestly the best solution I have found as well. The ability to include the constants in pattern matching, especially for me when defining functions, makes macros amazing for this. Oct 8, 2018 at 16:34
  • 1
    Why qoute do instead of do?
    – jmathew
    Jan 10, 2020 at 19:26
9

Elixir modules can have associated metadata. Each item in the metadata is called an attribute and is accessed by its name. You define it inside a module using @name value. And is accessed as @name

defmodule Example
  @site 'StackOverflow' #defining attribute

  def get_site do
    @site #access attribute
  end 
end

Remeber this works only on top level of a module and you cannot set a module attribute inside a function definition.

2

I wanted to add how I've started doing constants, which is similar to @Onorio Catenacci's answer, but uses quoting:

defmodule IbGib.Constants do

  @doc """
  Use this with `use IbGib.Constants, :ib_gib`
  """
  def ib_gib do
    quote do
      defp delim, do: "^"
      defp min_id_length, do: 1
      # etc...
    end
  end

  @doc """
  Use this with `use IbGib.Constants, :error_msgs`
  """
  def error_msgs do
    quote do
      defp emsg_invalid_relations do
        "Something about the rel8ns is invalid. :/"
      end
      # etc...
    end
  end

  @doc """
  When used, dispatch to the appropriate controller/view/etc.
  """
  defmacro __using__(which) when is_atom(which) do
    apply(__MODULE__, which, [])
  end
end

And then you use it like this at the top of the module where you want to consume them:

use IbGib.Constants, :ib_gib # < specifies only the ib_gib constants
use IbGib.Constants, :error_msgs

# ... then in some function
Logger.error emsg_invalid_relations

I got this with how Phoenix does import/use clauses with MyApp.Web. I'm nowhere near an Elixir expert, but with this method you can import just those constants that you want and you don't need to prefix them with any namespacing/scoping. This way, you can pick and choose the individual groups of constants easily.

With straight functions, (I think) you would have to break them up into multiple modules and then import the module.

I don't know the optimization ramifications of this vs direct module functions, but I thought it was pretty neat - especially for practice between the various ways to "import" things in Elixir (import, use, alias, require is very confusing as a beginner coming from other languages where this is a single using or import statement).

EDIT: I've changed the constant def declarations to defp. This is because when multiple modules import the constants file, there is an ambiguity conflict. Changing them to privately scoped functions avoids this conflict. So each module has its own "private copy" of the same constant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.