New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Restrict Enum values to numerical expression & Allow value access from macro #5903
Comments
Why wouldn't an enum for an fixed set of strings make sense? |
Enum is already restricted to integer type: enum Foo : String # Error: enum base type must be an integer type
Bar = "a"
end
puts Foo::Bar To me it doesn't make sense because an enum's type should be a basic type, not a reference/object/etc.. @asterite may help here as you wrote the restriction? |
Because enums are represented as the numbers you assign as the values in the binary representation. |
This issue talks about many things. Could we have separate issues for separate topics please? |
@asterite |
Oh, I misunderstood. I can't see a need for Enum.size nor for this. It's too complex. Just hatdcode the number of enum members somewhere, after all it's just to save a few allocations. |
Actually this issue has no raison d'etre, the feature I explain is already available in the compiler, shame on me I didn't see it... It's a bit funny @asterite because it's you who implemented this some time ago 😃 (no offense, it's very well done, I'm learning so much from what you did on Crystal 👍 ) Const/Enum field value interpretation: 9130ec71ce |
While trying to do #5743, I noticed there's no way to get en enum member's value from macros.
I need this to be able to calculate at compile time the number of unique enum members, but if I can't access there value I can't do this.
Given the following example:
In macros, we currently have
TypeNode#constants
which will give the list of constant names ([One, Two, OneAgain]
). I could useTypeNode#constant
to access each enum member's valueASTNode
, but then I have a problem withTwo = 1 + 1
andOneAgain = One
, I need to evaluate the value!Discussing it with @RX14, we need something like:
MathInterpreter
.What do you think?
Also, currently an enum type in macros is
TypeNode
, which mean we have to shareTypeNode
macro methods, which aren't always relevant to an Enum. I already noticed that for many other things, maybe this should be kept as a separate issue.The text was updated successfully, but these errors were encountered: