1
0
mirror of https://github.com/dart-lang/sdk synced 2024-07-08 12:06:26 +00:00
dart-sdk/sdk
Lasse R.H. Nielsen 18f37dd8f3 Add extension name getter on Enum.
We use an extension getter instead of an instance getter because
it doesn't conflict with any potential existing or future enums
which want an element named `name`.
Keeping the namespace for enum elements open is a priority.
We currently only reserve `index` and `values`.

BUG: https://github.com/dart-lang/language/issues/1511

Fixes language issue #1511, which is a long-standing request,
and should replace a number of alternative implementations
which are based on parsing the `toString()`.


This version has two fields on the shared superclass, the index
and private name, and has a separate `toString` for each `enum` class
which hard-codes that enum's class name.

An earlier version had both `"name"` and `"ClassName.name"` as fields
to be able to reuse the same `toString` method on all enum classes,
but that cost too much for JS compiled code.
Even having just `ClassName.` as a field and then combining inside
`toString` requires more code to create the enum instances.
Instead this version hardcodes the `ClassName.` string once
in the `toString` method, which means each enum class has its own
toString (which can *potentially* be tree-shaken then.)

This still tree-shakes slightly worse than the previous implementation
where every enum class had its own `index` and `_name` fields
independent of each other, which could then be tree-shaken independently.
However, the `index` was already made an interface member with the
addition of the `Enum` interface, so code which accesses `.index`
on something of the `Enum` supertype could prevent tree-shaking of
all enum classes' `index` fields.
Likewise any general access to the "name" of an enum would necessarily
do the same for the name.
This CL makes up for some of that by sharing more implementation
between enum classes.

DartVM AOT CodeSize impact: ~0.15% regression on gallery (little less on big g3 app)

TEST= New tests added to enum_test.dart

Change-Id: Id25334e6c987f470f558de3c141d0e3ff542b020
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/210480
Commit-Queue: Lasse R.H. Nielsen <lrn@google.com>
Reviewed-by: Stephen Adams <sra@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2021-09-22 14:20:16 +00:00
..
bin Deprecate top level pub. 2021-08-24 08:34:58 +00:00
lib Add extension name getter on Enum. 2021-09-22 14:20:16 +00:00
api_readme.md Update the API docs readme 2020-01-16 21:23:09 +00:00
BUILD.gn Deprecate top level pub. 2021-08-24 08:34:58 +00:00