apoorv-2204

apoorv-2204

Shared Common Constants/Errors Across the Application

What is the best way to define Elixir App wide constants and errors?.In languages like go lang we export and import constants and errors.

What is the idiomatic or best, the De facto industry standard of Elixir community or generally design pattern for it?

requirements: we should be able to match them in guards and clauses, it should be as similar to normal variables in elixir function.What if we want to have constants for UI and backend at the same time, Like for an atom UI will show a String, in backend a integer.

ps: I found this article from 2016.

And some examples from timex lib, but cant use them in guard or clause

Most Liked

ImNotAVirus

ImNotAVirus

I had replied to your old post and I’m reposting my reply but with a few additions.

For many projects, I’ve often needed to use constants/enums. So I made a lib for it: SimpleEnum.

I’ll let you read the thread for more details, but to sum up:

  • Enums can be used in guards
  • There are no dependencies, as with EctoEnum for example.
  • the introspection system makes it easy to connect SimpleEnum to other libs

Here is an example from the documentation:

iex> defmodule MyEnums do
...>   import SimpleEnum, only: [defenum: 2]
...>
...>   defenum :color, [:blue, :green, :red]
...>   defenum :day, monday: "MON", tuesday: "TUE", wednesday: "WED"
...> end

iex> require MyEnums

iex> MyEnums.color(:blue)
0
iex> MyEnums.color(0)
:blue
iex> MyEnums.day(:monday)
"MON"
iex> MyEnums.day("MON")
:monday
JEG2

JEG2

Author of Designing Elixir Systems with OTP

I’m also interested in hearing answers to this, especially for options that don’t spread compile time dependencies far and wide.

dimitarvp

dimitarvp

To be fair I’d just do this:

defmodule MyApp.UnixErrors do
  defmacro __using__(_opts) do
    quote do
      @bad_argument :badarg
      @invalid :einval
    end
  end
end

defmodule MyApp.UserModule1 do
  use MyApp.UnixErrors

  def hello(), do: @bad_argument
end

This too is some level of compile-time dependencies but they are flat, there are no cycles or deep graphs involved.

dimitarvp

dimitarvp

Have you tried? :wink:

dimitarvp

dimitarvp

Ah, you mean the module attributes can’t start with a capital letter. Can’t help you there, but I don’t see why would that be a hard requirement for you either.

Where Next?

Popular in Questions Top

pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
sergio_101
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
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement