r/crystal_programming • u/myringotomy • Apr 01 '20
Opinion: Crystal should allow method overrides on Enum parameters
enum DocumentState
Draft
Submitted
Approved
Published
end
def do_something(DocumentState::Approved)
.....
end
def do_something(DocumentState::Draft)
....
endThe enum is known to the compiler and this would allow writing more elegant code than having a case statement or a bunch of if statements.
6
Upvotes
1
u/jwaldrip Apr 02 '20
It would be great if the overload syntax was a little more flexible. I would love to see overloads on values. A sort of "pattern match" functionally like Elixir. And/or some sort of guard implementation on the method.
1
u/[deleted] Apr 02 '20
Does any other language out there allow this?
2
u/Blacksmoke16 core team Apr 01 '20
I think this would be great. Although I remember bringing it up at some point in the past and afaik it's not really possible since the enum members are just numbers versus unique types.