-
Notifications
You must be signed in to change notification settings - Fork 39
Open
Description
Currently Enum values are represented by integers in Elixir.
enum Color { RED = 1, GREEN = 2, BLUE = 3 }
struct Pixel {
1: optional Color color
}require Color
assert Color.RED == 1
pixel = %Pixel{color: Color.RED}
# Is equivalent to:
pixel = %Pixel{color: 1}
case pixel.color do
Color.RED -> :ok
endIt seems like it would be nicer if enums were represented by atoms.
pixel = %Pixel{color: :RED}
case pixel.color do
:RED -> :ok
endPros
- If you inspect/print a value, it's confusing to see a number and have to go find the thrift definition and map the number back into a name that you can reason about. An atom is directly readable.
> IO.inspect user
%User{status: 7} # what is that?...versus...
> IO.inspect user
%User{status: :INACTIVE} # OK makes sense-
Less generated code, faster compile times.
-
You don't have to require a generated enum module to use the values.
-
It's more Elixir-y, I think.
Cons
- No longer catches invalid enum values. However it didn't catch enums being used in the wrong context, so we need Dialyzer annotations either way for that.
thecodeboss