View Source Defconstant (defconstant v2.0.0)

Helper functions for defining constant values in your modules.

Usage

defmodule Foo do
  import Defconstant

  defconst comptime do
    # This will be evaluated at compile time
    Enum.sum([
      0, 2, 3, 4, 5, 6, 8, 12, 30, 32, 33, 34, 35, 36, 38, 40, 42, 43, 44, 45,
      46, 48, 50, 52, 53, 54, 55, 56, 58, 60, 62, 63, 64, 65, 66, 68, 80, 82,
      83, 84, 85, 86, 88
    ])
  end

  defonce runtime do
    2 * 1068 + 1
  end
end

Summary

Functions

Defines function that will be evaluated once, in compile time, and will return computation result.

Defines private function that will be evaluated in compile time.

Defines function that will be evaluated once, in runtime, and will cache the result.

Defines private function that will be evaluated once, in runtime, and will cache the result.

Functions

Link to this macro

defconst(call, opts)

View Source (macro)

Defines function that will be evaluated once, in compile time, and will return computation result.

Defined function can only be 0-ary.

Example:

defmodule MyConstants do
  import Defconstant
  defconst the_answer, do: 42

  def my_func do
    the_answer() * 2 # returns 84
  end
end
Link to this macro

defconstp(call, opts)

View Source (macro)

Defines private function that will be evaluated in compile time.

For details see defconst/2.

Link to this macro

defonce(call, opts)

View Source (macro)

Defines function that will be evaluated once, in runtime, and will cache the result.

Defined function can only be 0-ary.

Link to this macro

defoncep(call, opts)

View Source (macro)

Defines private function that will be evaluated once, in runtime, and will cache the result.

For details see defonce/2.