Shared Common Constants/Errors Across the Application

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
2 Likes