benperiton
Hi,
I’m porting an old PHP application over to Elixir and Phoenix, and I’m struggling to figure out the best way to transfer the constants that currently exist.
At the moment there are multiple files with various constants used throughout the app, for instance:
<?php
namespace Shared\Consts;
class System
{
const AUDIT_ENTITY_REV_TYPE_DELETE = 301001;
const AUDIT_ENTITY_REV_TYPE_INSERT = 301002;
const AUDIT_ENTITY_REV_TYPE_UPDATE = 301003;
const AUDIT_LOG_ACTION_CREATE = 301004;
const AUDIT_LOG_ACTION_CUSTOM = 301005;
const AUDIT_LOG_ACTION_DELETE = 301006;
const AUDIT_LOG_ACTION_EDIT = 301007;
const SOURCE_LVL1_API = 301023;
const SOURCE_LVL1_HTTP = 301024;
const SOURCE_LVL1_EMAIL = 301025;
const SOURCE_LVL1_SMS = 301026;
const SOURCE_LVL1_UNKNOWN = 301027;
const SOURCE_LVL2_STAFF_PANEL = 301028;
const SOURCE_LVL2_STAFF_APP = 301029;
const SOURCE_LVL2_RESELLER_PANEL = 301030;
const SOURCE_LVL2_RESELLER_APP = 301031;
const SOURCE_LVL2_AFFILIATE_PANEL = 301032;
const SOURCE_LVL2_CUSTOMER_PANEL = 301033;
const SOURCE_LVL2_CUSTOMER_APP = 301034;
const SOURCE_LVL2_UNKNOWN = 301035;
}
Then in other files I can do:
<?php
use Shared\Const\System as SysConst;
if (SysConst::SOURCE_LVL1_API === $someOtherVariable)
{
echo 'Starts at API';
}
Is there a way to do something similar in Elixir? Or is there a better way to store lots of constants that could be the same name, but relate to different areas?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security










Most Liked- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
smpallen99
I have a macro for reusing constants across multiple modules. The benefit being that they work in matches and in guards.
sasajuric
I would definitely go for the approach from @michalmuskala. One problem with maps is that it only supports one way conversion (converting a const into an integer). If you need to convert an integer into a const, you’ll have to build a reversal map. This can be easily done, even during compilation time, but then the code becomes as complex if not more than Michal’s version.
Moreover, I’m not completely sure whether this map is stored in the so called “constant pool”, and if it’s not, then the performance might suck. But even if this is not the case, my previous point stands, and I would personally go for Michal’s solution.
Using that approach, you could have something like:
And now you can do e.g.
Const.encode(:source_lvl1_api), orConst.decode(301023)to perform atom ↔ integer conversions.So what Michal is trying to tell you is that as soon as you take some input from say HTTP request, or the database, you invoke
Const.decodeto convert it into an atom. Then in the rest of your code, you just deal with atoms (e.g.:source_lvl1_api), so the code is ridden of magical numbers. Likewise, if you need to send a response to some client, or store to the database, you performConst.encodeto convert the atom into a corresponding integer.michalmuskala
The usual approach would be to use atoms inside the system, e.g.
:source_lvl1_apiand convert to/from integer encoding on the system boundary, if needed. This makes it easy for debugging and introspection since at runtime you have readable atoms, instead of opaque integer values flying around. You can generate the conversion functions easily with a sprinkle of macros:Last Post!
beamologist
Thank you so much, good sir! I will shamelessly steal this, but mention your name for the glory