Skip to content
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

Bidirectional (TypeScript-style) enums #8

Open
fatcerberus opened this issue Oct 18, 2018 · 4 comments
Open

Bidirectional (TypeScript-style) enums #8

fatcerberus opened this issue Oct 18, 2018 · 4 comments

Comments

@fatcerberus
Copy link

Can enum be bidirectional like in TypeScript? For example:

enum FatAnimals
{
    Cow,
    Gorilla,
    Pig
}

console.log(FatAnimals.Pig);  // number 2
console.log(FatAnimals[2]);  // string "Pig"

This is nice for serialization as it gives you an easy way to serialize the human-readable enum key while passing it around as a number internally. Admittedly this is only useful for Number-based enums, and the use case in question is solved by using a String-based enum, but numbers are more optimizer-friendly (not all JS engines intern strings so string comparison can be more expensive than number comparison).

@rbuckton
Copy link
Owner

The enum design in this proposal is bidirectional, but requires the use of an enum API to accomplish this. Having an external API is more reliable for string-valued enums like this:

enum E {
  red = "blue",
  blue = "green"
}

In TypeScript, this results in an enum that is not bidirectional.

Instead you would do the following (using your example above):

console.log(FatAnimals.Pig); // number 2
console.log(Enum.getName(FatAnimals, 2)); // string "Pig"

@fatcerberus
Copy link
Author

Huh, learn something new every day - I actually wasn't aware TypeScript allowed enumeration members to be anything other than ints (as in C), so them allowing string-valued keys is news to me. I can see why your example wouldn't be bidirectional in TS. 😄

I definitely agree having an explicit API for this is much more flexible and robust; I dislike the verbosity of a function call here (I'd prefer some kind of operator I think) but realistically doing a value->key mapping is probably not a common enough thing for it to matter in the grand scheme of things. I only really foresee it being done for serialization purposes.

To be honest, I'll just be happy if enum ever makes it into the language at all, whatever form it takes. It's a bit amusing to me that enum has been a future reserved word practically forever yet JavaScript remains (I think) the only mainstream "curly brace" language not to include proper enumerations in the syntax! 😆

@fatcerberus
Copy link
Author

fatcerberus commented Oct 20, 2018

🚲🏚

A bit a bikeshedding here, but looking at that API listing, since Enum.keys() uses the "key" terminology, it seems a bit inconsistent that the API to get a specific key is called getName... maybe it could be something like keyOf instead. Enum.keyOf(some_enum, x) feels less awkward to me than Enum.getName(some_enum, x).

I would almost expect something called "getName" to return the name of the enumeration rather than a specific key.

@j-f1
Copy link

j-f1 commented Oct 21, 2018

Perhaps getKey or keyFor?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants