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
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries.
offset-based pagination with...
New
I needed to reuse React components from my Chrome extension in my Phoenix/LiveView backend. I noticed that for Svelte/Vue, there are live...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi all!
I want to present a small library which provides a mix task for generating an Entity-Relationship Diagram for Ecto schemas.
You...
New
Hello
Published a new library - ProcessHub!
ProcessHub is a library designed to manage process distribution within the Elixir cluster. ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
It’s not that it’s vocabulary is too advanced. It’s something worse.
I get lost trying to follow even a paragraph written by Claude. It’...
New
This showed up on my feed.. anyone heard of it? Just hype?
Ox Alpha is a reasoning model designed for coding, sustained ag...
New
Today we’re releasing Oban for Python. Not an Oban client in Python. Not a pythonx wrapper embedded in Elixir. Nope, it’s a fully operati...
New
@hugobarauna, Dr. Dimitrios Koutmos (my brother) and I (Alex Koutmos) have been hard at work on writing a book on how you can use Elixir ...
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
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #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.