Skip to main content
TopMiniSite

Back to all posts

How to Define Shared Constant In Elixir?

Published on
4 min read
How to Define Shared Constant In Elixir? image

Best Elixir Programming Books to Buy in October 2025

1 Elixir in Action, Third Edition

Elixir in Action, Third Edition

BUY & SAVE
$47.40 $59.99
Save 21%
Elixir in Action, Third Edition
2 Programming Elixir ≥ 1.6: Functional |> Concurrent |> Pragmatic |> Fun

Programming Elixir ≥ 1.6: Functional |> Concurrent |> Pragmatic |> Fun

BUY & SAVE
$32.87 $47.95
Save 31%
Programming Elixir ≥ 1.6: Functional |> Concurrent |> Pragmatic |> Fun
3 Elixir Patterns: The essential BEAM handbook for the busy developer

Elixir Patterns: The essential BEAM handbook for the busy developer

BUY & SAVE
$49.00
Elixir Patterns: The essential BEAM handbook for the busy developer
4 Elixir Programming Mastery: An In-Depth Exploration for Developers

Elixir Programming Mastery: An In-Depth Exploration for Developers

BUY & SAVE
$29.99
Elixir Programming Mastery: An In-Depth Exploration for Developers
5 The Art of Elixir: elegant, functional programming

The Art of Elixir: elegant, functional programming

BUY & SAVE
$39.99
The Art of Elixir: elegant, functional programming
6 Engineering Elixir Applications: Navigate Each Stage of Software Delivery with Confidence

Engineering Elixir Applications: Navigate Each Stage of Software Delivery with Confidence

BUY & SAVE
$60.71
Engineering Elixir Applications: Navigate Each Stage of Software Delivery with Confidence
+
ONE MORE?

In Elixir, you can define shared constants by using the [@](https://ubuntuask.com/blog/how-to-get-struct-name-in-elixir) symbol followed by the constant name. These shared constants can be used across different modules within the same project. For example, you can define a shared constant named PI in a module like this: @PI 3.14159. This constant can then be accessed and used in other modules by referring to ModuleName.PI. This approach helps in keeping the codebase organized and consistent by centralizing commonly used values.

How to enforce immutability for shared constants in Elixir?

In Elixir, immutability is enforced by default for all data structures. However, if you want to ensure immutability for shared constants specifically, you can achieve that by following these best practices:

  1. Use the @moduledoc attribute to clearly document and communicate that certain values are meant to be constant and not modified.
  2. Use module attributes (@constant_name) to define shared constants at the module level. This way, the constants are accessible across the module but cannot be modified once set.
  3. Avoid using global variables for shared constants, as they can be modified from anywhere in the application. Instead, encapsulate constants within modules to restrict access and enforce immutability.
  4. Define constants as private functions or modules to prevent accidental modification from external code.
  5. Use pattern matching and guards in functions to ensure that constants are not modified or overwritten.
  6. Use structs or maps to define complex constants, as they provide a more structured way to store and access constant values.
  7. Test your code thoroughly to ensure that shared constants remain immutable and do not change unexpectedly.

By following these practices, you can enforce immutability for shared constants in Elixir and ensure that your code remains maintainable and reliable.

How to handle dependencies between shared constants in Elixir?

One way to handle dependencies between shared constants in Elixir is to define them in a separate module and use that module wherever the constants are needed. This way, all the constants are centralized in one place and can be easily updated if needed.

Here's an example of how you can define a module to hold shared constants:

defmodule SharedConstants do defmodule Colors do @primary_color :blue @secondary_color :green

def primary\_color, do: @primary\_color
def secondary\_color, do: @secondary\_color

end

defmodule Sizes do @button_size "medium" @header_size "large"

def button\_size, do: @button\_size
def header\_size, do: @header\_size

end end

You can then use these constants in other modules by calling the appropriate functions from the SharedConstants module:

defmodule ExampleModule do import SharedConstants.Colors, only: [primary_color: 0] import SharedConstants.Sizes, only: [button_size: 0]

def example_function do IO.puts "Primary color is: #{primary_color()}" IO.puts "Button size is: #{button_size()}" end end

By organizing your shared constants in this way, you can easily manage dependencies between them and keep your codebase clean and maintainable.

How to update the value of a shared constant in Elixir?

In Elixir, constants are typically defined using module attributes. These attributes are set at compile-time and cannot be changed at runtime.

If you need to update the value of a "constant" at runtime, you can use a process-based state storage such as an Agent or GenServer. Here's an example using an Agent:

  1. Define an Agent module with a function to update the value:

defmodule ValueAgent do use Agent

def start_link(initial_value) do Agent.start_link(fn -> {initial_value} end, name: __MODULE__) end

def update_value(new_value) do Agent.update(__MODULE__, fn {current_value} -> {new_value} end) end

def get_value do Agent.get(__MODULE__, fn {value} -> value end) end end

  1. Start the Agent with an initial value:

ValueAgent.start_link(10)

  1. Update the value:

ValueAgent.update_value(20)

  1. Get the current value:

ValueAgent.get_value()

This approach allows you to have a shared state that can be updated at runtime, making it possible to change the value of a "constant" in a controlled way.