-
Notifications
You must be signed in to change notification settings - Fork 47
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
Add Enums #299
Comments
Pretty good. I think iteration could be improved to use a "hidden" iterator and immediately getting the value. E.g. I think it would be valid:
|
Thanks for the write-up! I had a few thoughts:
|
@elsassph your suggestion makes a ton more sense and might be easier to implement, so I've included it in the proposal. @TwitchBronBron regarding your first point: I currently work on an app where we use that |
For automatic string values you may want to include the enum name, e.g. |
I'm not overly fons of that idea. You shouldn't normally be mixing and matching different enums. We can catch that kind of thing during validation. Typescript doesn't do it this way either if I remember correctly. |
If you stick with my proposal, you could easily write a bsc plug-in to auto generate those functions to hold you over until you get totally converted to bsc. |
I was thinking about them while modifying the proposal, but I didn't know if that could be done given my inexperience with bsc. That gives a bit more confidence, thank you.
This actually makes me pivot and wonder if we should align the proposal to have enums be numeric by default, instead of auto-generated strings. It would bring the feature more in line with what Typescript, C#, and other languages already do: enum NumericDirection
Up ' 0
Down ' 1
Left ' 2
Right ' 3
end enum
enum NonZeroNumericDirection
Up = 1 ' 1
Down ' 2
Left ' 3
Right = 3 ' 3 (allows duplicate values)
Front ' 4 (assumes "previous value + 1")
end enum
enum StringDirection as String
Up = "up"
Down = "down"
Left = "left"
Right = "right"
Front = "right" ' (valid: allows duplicate values)
Front = "front" ' (error: duplicate keys)
Bottom ' (error: initialiser needs to be specified when enum is not numeric)
end enum |
I've been using typescript as a model/inspiration for many things in bsc, since BrightScript shares many similarities with JavaScript. I like this idea of aligning with their implementation of enums. |
Agreed that ultimately using int by default should have better performance. |
@TwitchBronBron @elsassph proposal updated. |
This looks great! Another thought: we don't need the Do we see any value in supporting types other than |
FWIW from the perspective of a developer that intends to make heavy use of this feature, and for a first iteration, I don't see any problems in restricting enums to |
This proposal is in progress. Feedback is welcome.
Basic declaration
By default, enum values are numeric and start from zero, with each subsequent key assuming the previous value + 1:
You can override any of the inferred values:
You can also specify string values. However, all values will need to be initialized, and all of them need to be strings:
Heterogeneous enums are not allowed in order to avoid "type mismatch" runtime errors.
Usage Examples
Value assignment and comparisons
Where possible, Brighterscript will make use of compile-time constants.
As method argument and return type
Enum iteration
Sometimes it's useful to be able to iterate through all values:
The text was updated successfully, but these errors were encountered: