bcc icon indicating copy to clipboard operation
bcc copied to clipboard

Enums: Get "index" or "length" ?

Open GWRon opened this issue 2 years ago • 13 comments

Hey,

as @scaremonger just wrote about it on discord ...

SuperStrict
Enum WEEKDAY; MON,TUE,WED,THU,FRI,SAT,SUN; End Enum

Local today:WEEKDAY = WEEKDAY.MON
Print "Today is "+today.toString()

Local tomorrow:WEEKDAY = WEEKDAY( ( today.ordinal() + 1 ) Mod 7 )
Print "Tomorrow is "+tomorrow.toString()

Local yesterday:WEEKDAY = WEEKDAY( ( today.ordinal() + 6 ) Mod 7  )
Print "Yesterday was "+yesterday.toString()

How to retrieve the "index" of an enum element, or the total amount of elements in an enum?

So in this case:

print WEEKDAY.length 'should print 7
print WEEKDAY.MON.index 'should print 0

Would that be ... doable ... and would that be ... good to have?

For now we would have to rely on WEEKDAY.Values() (array) to retrieve the length, and today.ordinal() would return the numeric value (so when starting from "0" and not "sparse", it could work as array index).

GWRon avatar Oct 24 '22 17:10 GWRon