I was writing some code using an enum for timing different stages of code. Something like:
enum TimeStages {A, B, C}
var enumArr: [TimeStages.A..TimeStages.C] real;
Every time I added a new timing stage, I found it a little annoying to update the enum, and the array declaration. I found myself wondering if we could support first()/last() method like we have for ranges/domains.
That way you could have something like:
enum TimeStages {A, B, C}
var enumArr: [TimeStages.first()..TimeStages.last()] real;
I'm open to other ways of writing this sort of pattern, this is just what came to mind first.
Huh, there's apparently a chpl_enum_first function. That's not the interface we'd like, but it may be a starting point for the compiler implementation.
I think
var enumArr: [TimeStages] real;
or
var enumArr: [{TimeStages}] real;
would be elegant ways to write it.
it may be a starting point for the compiler implementation.
This will be really easy to implement. If it'd benefit you to have it soon, let me know.
I'm in no rush for this. I stumbled across chpl_enum_first while looking at something else and I was surprised to see that it was a thing so I wanted to note it here.
When I said "this will be really easy", I meant "it should be a really easy compiler change." Turns out it's even easier than I was guessing (such that even a performance-tuning programmer could've pulled it of... :D ). See PR #15098
Paul, I feel sortof intrigued by and sortof scared by your suggestion, so did not tackle it here, and am not enough of a champion to fork it off into its own feature request issue myself. But I wouldn't have any qualms if you were to do so.
The following code uses the .first and .last keyword identifiers introduced by #15098 as elements in the enum itself. Is this usage intended?
enum E {
a,
first,
b,
last,
c,
}
for e in E {
writeln(e);
}
writeln("----");
for e in E.first..E.last {
writeln(e);
}
# chpl version 1.23.0 pre-release (8260f8962)
a
first
b
last
c
----
first
b
last
While arguably correct, it could deceive a reader of the code if they don't know the definition of the enum.
I definitely didn't intend to prevent someone from writing this, in the sense that I didn't want to make the new methods prohibit the use of any specific identifiers within the enum. Here's a similar, pre-existing example that's maybe slightly more worrisome since it doesn't have an obvious workaround:
enum color {red, green, size, blue};
writeln(color.size);