Onor.io
Three Ways To Share Values Between Modules
A new REPL on Replit to help demonstrate three techniques to safely share values between modules. DRY up your code in a safe way!
defmodule MyMath do
@moduledoc """
This repl documents three ways to share values between modules.
The question may occur to the reader--why would I want to share values between modules? It's a valid question. It is often the case that we need to share some common piece of logic among different contexts. For example if we use a regular expression (regex) to parse, for example, an address, we want to insure we use the same regex every place we need to parse an address. We can certainly copy/paste the regex but that leads to a lot more work for later developers who if they find they need to modify the regex must insure they find all the uses of the regex and modify all of them. This becomes an even larger issue when you have teams of developers, potentially distributed across geography. Communication is already a problem in that case and it's far more likely that someone will miss something.
The following repl is structured as one file with two modules to make it easy to run and observe the behavior. The Main module gives examples of how to use the shared values across modules.
You can try the following in the console to assure yourself that this all works as expected:
## Examples
iex> radius = 3.0
3.0
iex> Main.circle_area(radius) == Main.circle_area_alternate(radius)
true
iex> Main.circle_area_macro_approach(radius) == Main.circle_area(radius)
true
"""
#First symbolic constant technique: module attribute
Module.register_attribute(__MODULE__, :pi, persist: true)
@pi 3.141592653
#Second symbolic constant technique: function to return constant value
def pi, do: 3.141592653
# this could also be:
# def pi, do: @pi
#Third symbolic constant technique (many thanks to Paul Schoenfelder for this): macro
defmacro __using__(_) do
quote do
@pi 3.141592653
end
end
end
defmodule Main do
defp get_pi do # module attribute approach
[pi] = MyMath.__info__(:attributes)[:pi]
pi
end
def circle_area(radius) when is_float(radius) do
MyMath.pi() * radius * radius #constant function approach
end
# or
def circle_area_alternate(radius) when is_float(radius) do
get_pi() * radius * radius
end
# or
use MyMath
def circle_area_macro_approach(radius) when is_float(radius) do
@pi * radius * radius
end
end
Most Liked
Onor.io
Thanks for moving this to the right place. I tried posting under Learning Resources but I couldn’t.
Yeah I figured people would just click the Run button and then try the examples but I can see how that may be not as obvious as I would have hoped.
1
Popular in Guides/Tuts
So my main OS is Windows, I do must of my work with it, Elixir and vscode elixirls works just fine when you’re working only with elixir, ...
New
Hi folks,
Just a short instruction. Maybe it will help somebody.
Install boostrap with deps and Sass:
cd assets
npm install jquery ...
New
Hi all!
Just want to share a small code snippet which allows writing CASE expressions using macro which is similar to cond.
Here is an ...
New
Hi everyone,
Just wanted to say that the new Self-referencing many to many guide is now up on the official Hex docs (at least I just not...
New
Another cool plugin for Neovim, GitHub - jmbuhr/otter.nvim: Just ask an otter! 🦦 · GitHub makes it possible to run linters for embedded c...
New
I just wrote a simple guide on how you can setup a productive elixir development environment in vim.
Its really easy, just a few steps. ...
New
I have published an elixir project with using Travis CI.
I would like to share some tips & thoughts that I was getting through this ...
New
I’ve spent some time understanding how to do hot code reloading with releases built using mix release, and here I’d like to detail the st...
New
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
Hi! I recently finished adding authentication to my Phoenix API, so I wanted to share what I learned.
I haven’t created authentication f...
New
Other popular topics
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode.
The solution seems to be, in a hyphena...
New
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
15:22:35.803 [error] gen_event {lager_file_backend...
New
Hello all!
I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
Hi everyone,
One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
lets say i have a sample like
a = 20; b = 10;
if (a > b) do
{:ok, "a"}
end
if (a < b) do
{:ok, b}
end
if (a == b) do
{:ok, "equa...
New
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
I would like to know what is the best IDE for elixir development?
New
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #javascript
- #code-sync
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









