Sdk: Add `name` instance property to enum values

Created on 25 Nov 2014  Â·  15Comments  Â·  Source: dart-lang/sdk

_This issue was originally filed by @seaneagan_


With the current spec it's difficult to get the name of an enum value, since the toString() result is prefixed with EnumClass.:

enumName(enumValue) {
  var s = enumValue.toString();
  return s.substring(s.indexOf('.') + 1);
}

It would be nice to just be able to do:

enumValue.name

area-language core-l type-enhancement

Most helpful comment

What about a getter simpleName or a method getName()?

All 15 comments

I agree that it would be convenient with a way to get the name, and it's a problem that the name exists, but is only available by parsing the toString (Bloch said something about that in Efficient Java).
However, an argument against it is that every member you add to all enums will also block that name for use as an enum item:

enum EnvelopeEntry { name, address, city }

would fail if "name" is a member of enum instances.


_Added Area-Language, Triaged labels._

_This comment was originally written by greg....@gmail.com_


Also worth considering - what will enum.toString() print once minified? My guess is the minified name not a readable one.

So if you need to keep the name string unminified in js, then you should probably use a class with a const constructor and a name field.

_This comment was originally written by @seaneagan_


The spec seems to imply that the enum name is preserved for the toString() output, but not sure if the implementations honor that.

I guess the name conflict is due to dart's having a single namespace for instance and static members. To avoid that, could solve this via an EnumInstanceMirror in dart:mirrors, but of course that has drawbacks.

_Set owner to @gbracha._
_Added Accepted label._

_Added C11 label._

What about a getter simpleName or a method getName()?

No news about this?

I also want this badly. @leafpetersen @munificent @eernstg @lrhn – is this more a language thing or a library thing?

It can be either.
The language specification states how an enum class looks.
If we just add a static helper method, say Object.enumName, then it's just a library change (even if it has fundamental effect on how enums are compiled).
We probably wouldn't, it's more likely that we'd add a superclass for enum classes (called Enum perhaps), and then add the static helper on that class (say String name(Enum value)).

So, most likely a language issue in some way.

Adding simpleName on the enum object will prevent enums from having that identifier as a name. We don't want to add anything to enum classes.

I'm still fundamentally opposed to exposing the source name as a string. It's just bad style, and we should never do (well, have done) that. It's reflection.
Sadly, we have already exposed the name in the toString, and then we might as well make it directly accessible somehow. (I believe Josh Bloch said something about that once).

I'm sure it's too late to change this, but a simple solution would have been to have toString() return the bare name instead of EnumClass.enumName. I can't think of any cases where including the class name in there was useful.

Sadly, I have quite a bit of code (mostly in json_serializable) that
assumes you have ClassName.enumName :-/

Could we add a synthetic static method on enum? enum.name(Something.value)

On Mon, Feb 4, 2019 at 3:39 PM Bob Nystrom notifications@github.com wrote:

I'm sure it's too late to change this, but a simple solution would have
been to have toString() return the bare name instead of EnumClass.enumName.
I can't think of any cases where including the class name in there was
useful.

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/dart-lang/sdk/issues/21712#issuecomment-460457513,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AABCihPcP-0nynmaMkgQbwVijcLRO1mjks5vKMSygaJpZM4E4FCV
.

We'd have to add an enum to add that to.

If we don't provide a superclass to enums, then you could just use:

String enumName(Object o) {
  var text = o.toString();
  return text.substring(text.indexOf(".") + 1);
}

What about improving support for old-style enums in the language (switch) and the analyzer (auto completion), (and perhaps some specialized code generation support) instead of tweaking Dart enums which are too limited anyway for all scenarios except the most basic ones?

The one-liner to get the name:

String enumName(Object o) => o.toString().split('.').last;

Just made a package for this one, also handles optional camel case parsing for easy display to user:

https://pub.dev/packages/enum_to_string

Was this page helpful?
0 / 5 - 0 ratings