Hi all,

after reading the topic at 
http://stackoverflow.com/questions/33851536/how-do-you-define-constants-in-elixir-modules
 
I decided to use the approach taken by wxErlang. I found this to be a 
little verbose, especially since I also needed a list of all constants and 
an easy way to convert from the integer value to the associated atom.

So I created the const package, which allows you to write this:

defmodule Status do
  use Const, [:queued, :processed, :sent]
end

and obtain this

defmodule Status do
  def queued, do: 0
  def processed, do: 1
  def sent, do: 2
  def all, do: [queued: 0, processed: 1, sent: 2]
  def by_value(val) do
    # returns the atom from the integer value. In case of duplicated 
values, the fist
    # associated atom is returned
  end
end

You can also give a keyword list if you need specific values, and even a 
list where some elements are just atoms and some are tuples. The behavior 
in this case will be like for C enums.

More details at: https://github.com/bitgamma/const

Hope it can be useful!

Regards,
Michele Balistreri
Bitgamma OÜ

-- 
You received this message because you are subscribed to the Google Groups 
"elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elixir-lang-talk/76121992-3a22-4211-8032-6ed997247349%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to