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
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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
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
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
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
- #hex
- #security
- #metaprogramming










Showing Posts 21 to 12- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
beamologist
Thank you so much, good sir! I will shamelessly steal this, but mention your name for the glory
ImNotAVirus
Yes it is.
Or you can use simple_enum (written by myself ^^) which does exactly the same thing with a few extra features.
Here is an example from the documentation:
apoorv-2204
Is it still valid today?
thiagomajesk
I recommend that you take a look at the EctoEnum library for insights.
It’s worth mentioning to people coming to this thread that after getting used to Elixir it feels more natural to pass atoms around, so that’s not a big deal. The minor drawback is refactoring; you’ll have to find-all/ replace-all instead of just hitting the refactor/ rename button in your editor (the outcome tends to be virtually the same).
In most cases, having good documentation on the available options is sufficient.
Also, I think you shouldn’t worry too much about having autocomplete for that. Instead, you can write documentation and typespecs for your public APIs to achieve a similar goal.
mrpola
I’m trying to achieve the same thing but with autocomplete functionality to know which atoms are available for use.
Would there be any performance drawbacks if I create a function for each key value pair?
I’m new to Elixir, sorry if this is a silly question
And then, when I need to use the error message
This way the editor offers me all available functions in the module as autocomplete
OvermindDL1
Just return a list of them then, no need to duplicate them.
Making a map of them can still be quite useful because it allows for O(log N) lookup instead of O(N) if you just need to verify it is valid or not (I generally do the values as just
truein that case because convention, but doesn’t really matter).Likewise, and baking them into a module like this is the way to do that, the values all get interned into the system and no GC will touch them and their linked usage anywhere will not trigger GC either, it’s very efficient.
thiagomajesk
Coming from a static language background it’s really hard to get around the “elixir-way”.
@OvermindDL1 What would be your approach if instead of integers you’d need a list of atoms?
Imagine if you needed to validate a list of valid countries; it seems a bit overkill (due to repetition) to just:
Instead of simply scattering atoms in the application, I like the idea to have a “single source of truth” to serve like self-documentation (mainly autocomplete). In that case, I’d typically use an enum or a class with static const properties in C#.
PS.: BTW I don’t like very much the idea of using loose JSON files in the project like @benperiton.
benperiton
Seems I’ve been spending too much time using things that live reload code, the files DO recompile if I change the JSON, I was just waiting for it to happen live
I’m sure there are things I can tweak in the below, but it seems to work fine for what I need right now.
So this is end result of all the suggestions (thanks again!)
benperiton
Thanks guys, I’m really liking where this has gone! So, I’ve tried to implement both of those ideas, and I now have
That makes a much nicer way to access the different files, so thanks for that suggestion @sasajuric! I’m not sure if I’m doing something wrong, but it doesn’t seem to recompile if I modify one of the JSON files?
I’m running it using
iex -S mix phoenix.serversasajuric
You could also reach for multiple “nested” modules. Basically, for each input json file name, create the module alias using
Module.concat, and then define the module dynamically. That way you could invoke sayConst.System.decode/encode, assuming the input file issystem.json.