Macro would also be possible, but in that case you would have to require the module to use its constants. I agree however that being useable in guards is a good point. PRs are always welcome.
Regarding allowing non-integer values, I do not know if it makes much sense. The entire point of defining constants this way is to be able to interface with external services/serialization. If you can use Elixir terms then you probably do not need this. Or am I missing something? On Tuesday, August 30, 2016 at 5:14:19 PM UTC+3, OvermindDL1 wrote: > > 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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/9ff0db4b-6181-421d-8ed4-f5da8b5910d4%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
