Declare:
defmodule MyApp.ViaFunctions.Constants do
def app_version, do: "0.0.1"
def app_author, do: "Felix Orr"
def app_info, do: [app_version, app_author]
def bar, do: "barrific constant in function"
end
Consume with require:
defmodule MyApp.ViaFunctions.ConsumeWithRequire do
require MyApp.ViaFunctions.Constants
def foo() do
IO.puts MyApp.ViaFunctions.Constants.app_version
IO.puts MyApp.ViaFunctions.Constants.app_author
IO.puts inspect MyApp.ViaFunctions.Constants.app_info
end
# This generates a compiler error, cannot invoke `bar/0` inside a guard.
# def foo(_bar) when is_bitstring(bar) do
# IO.puts "We just used bar in a guard: #{bar}"
# end
end
Consume with import:
defmodule MyApp.ViaFunctions.ConsumeWithImport do
import MyApp.ViaFunctions.Constants
def foo() do
IO.puts app_version
IO.puts app_author
IO.puts inspect app_info
end
end
This method allows for reuse of constants across projects, but they will not be usable within guard functions that require compile-time constants.