Thanks for your interest!
Short answer:
The two main differences are that SimpleEnum does not depend on Ecto (it has no dependencies) and it adds Enums at compile time, not just at run time.
Long answer:
As mentioned in its documentation on reflection, Ecto.Enum’s introspection is only at runtime.
I originally wrote SimpleEnum because I needed to be able to work with Enumeration at compile time (in guards for example).
For example, let’s say I have a file that looks like this:
# <access_type> <value>
1 value1
2 value2
1 value3
....
Now, let’s imagine that I only want to display values with access_type 1 (because they are users data). I can do something like that:
def print_value(1, value), do: IO.inspect(value)
def print_value(2, value), do: :ignore
But here, why are we only printing value with type 1 ? The code is not really explicit.
A better solution could be:
@user_type 1
@admin_type 2
def print_value(type, value) when type == @user_type, do: IO.inspect(value)
def print_value(type, value) when type == @admin_type, do: :ignore
Now the code is much more explicit. It is easy to understand that we do not want to display data belonging to an administrator.
But what if several modules need to define these access types. The best would be to define a module that exports these constants.
This is what SimpleEnum does:
defmodule MyEnums do
import SimpleEnum, only: [defenum: 2]
defenum :access_type, [user: 1, admin: 2]
end
def print_value(type, value) when type == access_type(:user), do: IO.inspect(value)
def print_value(type, value) when type == access_type(:admin), do: :ignore
Here, my example is probably not the best, because you can easily rewrite it without guard (with a case for example) but sometimes it is not possible.
Conclusion:
I think SimpleEnum is not intended to replace ecto_enum or the native Ecto.Enum module but rather to add compile time functionality to them.
In the documentation, there is an example where you can see SimpleEnum and the native Ecto.Enum working together.


















