ImNotAVirus
Hi everyone,
I have just published the first version of a library that I have been using in my projects for over a year now: SimpleEnum - a simple library that implements Enumerations in Elixir.
SimpleEnum was designed to be :
- fast: being based on a macro system, access to the Enum will be resolved at compile time when it is possible
- simple: the use of the library has been designed to be as simple as possible for a developer to use. In addition to providing Enums, it automatically defines their types and provides an introspection system.
Examples of uses can be found in the documentation : SimpleEnum — simple_enum v1.0.0
Proposals :
To improve SimpleEnum, I had several ideas like
- Add the
defenumpfunction in order to create private Enums: currently, all Enums are public - Add an
EnumValueErrorexception for developers who needrescue. Currently,ArgumentErroris used - Add a decorator
@allow_duplicate_values trueor@unique falseto allow duplicates values in Enum - Add macros
is_enum/1,is_enum_key/1andis_enum_value/1(eg.is_color(:red),is_color_key(:red)andis_color_value(:red)) for each Enum. An example of implementation can be found here.
What do you think? Would you need a feature in the list or another one?
Any issues, suggestions or contributions are welcome.
Cheers
Links:
- Hex.pm page: https://hex.pm/packages/simple_enum
- Documentation: https://hexdocs.pm/simple_enum
- Source code: https://github.com/ImNotAVirus/simple_enum
Trending in Announcing
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub.
Docs are at OpenaiEx User Gu...
New
WebAuthnLiveComponent WebAuthnComponents
See this post about renaming the package.
Passwordless authentication for Phoenix LiveView app...
New
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
I released Doggo, a collection of unstyled Phoenix components.
https://github.com/woylie/doggo
Features
Unstyled Phoenix components....
New
Hi everyone,
I’ve been working on this protobuf library for 3 years. We use it in the company I work for, EasyMile, to communicate with ...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
I’ll shortly be launching Text, a nascent text analysis library.
Current functionality
In this early version (not ready for prime time) ...
New
Other Trending Topics
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
New
What IDE or editor are you using for Elixir development?
Personally, I use Zed, and I really like it, but sometimes I wish there were a ...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #ai
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
thiagomajesk
Hi @ImNotAVirus! Cool project… Could you elaborate on how is
simple_enumdifferent from existing packages likeecto_enumand the nativeEcto.Enummodule? I’ve been usingecto_enumfor some time now and have been meaning to migrate to the new nativeEcto.Enum. I think a comparison would be great to understand the value proposition of your package. Cheers!ImNotAVirus
Thanks for your interest!
Short answer:
The two main differences are that
SimpleEnumdoes not depend onEcto(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
SimpleEnumbecause 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:
Now, let’s imagine that I only want to display values with
access_type1 (because they are users data). I can do something like that:But here, why are we only printing value with type
1? The code is not really explicit.A better solution could be:
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
SimpleEnumdoes: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_enumor the nativeEcto.Enummodule 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.Enumworking together.c4710n
Compile-time checking! Love it.
Thanks for sharing.
Sebb
How does simple_enum compare to ex_const?
ImNotAVirus
Thanks for the question.
I’ve never used ex_const so my answer probably won’t be complete but here are the main differences I noticed.
First of all, you must know that when writing
SimpleEnum, I got a lot of inspiration from how Enumerations work in C++. This explains some of my design choices like the fact that it is impossible to have 2 times the same key in an Enum.Difference 1: with
ex_const, an Enum can have duplicates keys and values.This is a valid code :
Here
SimpleEnumwill raise an exception at compile time:Difference 2:
ex_constdoes not support lists of keywords for creating EnumsWith
SimpleEnum, you can doDifference 3:
ex_constcan be used at compile time to get a value from a key but not the opposite.With
SimpleEnumyou can doDifference 4:
ex_constdoes not seem to have any introspection helper.