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
You may know https://ui.shadcn.com/, a UI component library for React. I really love it’s design style and components. I’ve built some co...
New
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries.
offset-based pagination with...
New
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub.
Docs are at OpenaiEx User Gu...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
The Chelekom project is a library of Phoenix and LiveView components generated via Mix tasks to fit developer needs seamlessly.
One of i...
New
Phoenix components for pagination, sortable tables and filter forms with Flop and (optionally) Ecto.
pagination
cursor pagination
sorta...
New
Please say hi to a new lib, Astro that aims to deliver easy-to-consume astronomy calculations of practical use. For now it only calculat...
New
Other Trending Topics
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
I am seeing a lot of aplications of Argumentum ad Vericundiam in software discussions. They do link some piece of writing and point us to...
New
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog
It says that Fly is going all-in on sprites, which is a worry ...
New
Hey folks,
I just published a post about Hologram’s funding and where the project goes next - the short version:
Curiosum as Main Spons...
New
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using.
We’re particularly inte...
New
I was thinking since Goatmire Elixir turned out pretty good I should maybe do another one. 30th of Sep - 2nd of Oct this year./
The firs...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #ai
- #iex
- #graphql
- #elixirconf-us
- #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.