I actually do use a lot of `defmacro something, do: 42` for enums operating 
with a remote system, this would be convenient.  :-)

Have you thought about making it a macro (be sure to escape the return in 
case they want the value to be a tuple or so), that way it can be used in 
more areas and in matching?

On Monday, August 29, 2016 at 11:52:44 PM UTC-6, Michele Balistreri wrote:
>
> 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 elixir-lang-talk+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elixir-lang-talk/de1268e5-109e-41d3-9117-39382628da9e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to