-
Notifications
You must be signed in to change notification settings - Fork 3
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
Alternative enum proposal #1
Comments
@rwaldron I am sorry, you should know I haven't given Ron much feedback on his proposal either. As most of my job is connecting disparate work streams together, I consider this a personal failure. Thanks for understanding. FWIW, the link to Rick's proposal is here: https://github.com/rwaldron/proposal-enum-definitions. |
@rwaldron, I've taken a look and here are some of my thoughts:
In general I agree with the semantics (block-scoped, no use before def, TDZ) but I don't necessarily believe this would be nested under LexicalDeclaration as I'd like to be able to decorate it in the long term. I'd liken it more like a ClassDeclaration, and would eventually like to support decorators.
In my proposal, given
This seems like it would conflict or be confusing with using an
This is doable, by moving much of the logic from %Enum% in my proposal to Also, I initially had an EnumExpression in this proposal but was advised to remove it for now to simplify the Stage 0 explanation.
This behavior is roughly analogous to my proposal, minus the
This behavior is generally identical.
I also had this originally but removed it to simplify the Stage 0 explanation.
I concur with these semantics, but left it out of the Stage 0 explanation.
This is something I would rather see done with a decorator (which I also want to support but have left out of the Stage 0 explanation). As why
It might be the case that we could argue for (1), with a built in decorator to achieve (2), which would greatly simplify this proposal: @Enum.asSymbols
enum Colors { Red, Yellow, Green, Blue }
typeof Colors.Red === "symbol"
Colors.Red.toString() === "Symbol(Colors.Red)" |
what about: enum(Number) Colors { Red: 255, Yellow: 255, Green: 255, Blue: 255 }
typeof Colors.Red === 'number' // Number(255)
enum(String) Colors { Red, Yellow, Green, Blue }
typeof Colors.Red === 'string' // String('Red')
enum(Symbol) Colors { Red, Yellow, Green, Blue }
typeof Colors.Red === 'symbol' // Symbol('Red')
enum(BigInt) Colors { Red: 255, Yellow: 255, Green: 255, Blue: 255 }
typeof Colors.Red === 'bigint' // BigInt(255) or something similar? |
You would need much more complex syntax to determine how you perform your mapping. If its just calling your EnumValueMap function with the SV of the name and the value, then your examples for One of the reasons I like the decorator approach is that it is much more flexible: @Enum.numbers enum Colors { Red = 0xff0000, Green = 0x00ff00, Blue = 0x0000ff }
typeof Colors.Red === "number"
@Enum.strings enum Colors { Red, Green, Blue }
typeof Colors.Red === "String"
...
@warnExceedSMI32 // user-supplied decorator
@Enum.flags
enum SymbolFlags {
None, // 0
FunctionScopedVariable, // 0x1
BlockScopedVariable, // 0x2
Property, // 0x4
// etc.
} An alternative that I've considered might be enum Colors extends Number { ... }
enum Colors extends String { ... } With a restricted set of allowed values in I'd still probably want to use a decorator to apply mappings for things like bitwise flag values though. |
i like |
Also, the enum Colors: int { ... }
enum Colors: ushort { ... } |
With the current proposal, the result of |
When |
@ljharb I don't think that relates to this issue. Would you mind adding a different issue for this? |
Filed as #3. |
I've been working on one as well, and was just about to start the legwork when I found this proposal. Not sure what's work taking, but here's some links in case: |
@rbuckton I have been working on one myself, too. The Git history is slim over there, but I've been trying various ways to see how best to fit enums into JS for a few years now. I'm a very heavy enum and bit mask user personally, and so I've gone through several different ways before finally, only recently, solidifying on any sort of pattern.
I also found myself slowly shifting how I was modeling related data with them:
And I also discovered a few patterns along the way:
Initially, I was against enums, but I wanted to experiment, so I started looking at the issues I've had with the various formats:
And that's why I decided to try to do a "best of all worlds" approach: I wanted to have my cake and eat it too. I wanted the uniqueness and of symbols for things like Flux/Redux reducers, the performance of numbers, the debuggability of strings, and a syntax that was cleaner than all three. In addition, I wanted to be able to specify data in a way that wasn't coupled to the enums themselves, something I already enjoyed being able to do with my style, but with some degree of verification that I didn't screw up and miss something on accident (which happens quite frequently with these kinds of tables when the names are unmarked). |
I've been waiting on feedback from @bterlson on an
enum
proposal that I asked for review on about 5 months ago. I was last under the impression that he wanted to share that with you before responding, and I've been self imposing a block on it until I got that review back. I understand that people are busy and this work isn't everyone's priority, but I've put a substantial amount of time and consideration into that proposal and I think it has some interesting ideas that are worth pursuing. If you're interested in combining the proposal, I'd be up for that, otherwise I will push forward independent of this proposal.The text was updated successfully, but these errors were encountered: