-
Notifications
You must be signed in to change notification settings - Fork 205
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
Specify that enums extend _Enum
from dart:core
.
#3671
Open
stereotype441
wants to merge
1
commit into
dart-lang:main
Choose a base branch
from
stereotype441:b_3665_2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -178,25 +178,9 @@ The recommended formatting of an `enum` declaration is to format the header (bef | |
|
||
The specification here does not specify *how* the index and name of an enum is associated with the enum instances. In practice it’s possible to desugar an `enum` declaration to a `class` declaration, as long as the desugaring can access private names from other libraries (`dart:core` in particular). | ||
|
||
The existing enums are implemented as desugaring into a class extending a private `_Enum` class which holds the `final int index;` declaration and a `final String _name;` declaration (used by the the `EnumName.name` getter), and both fields are initialized by a constructor. | ||
However, since the definition of least upper bound depends on the depth of classes in the class hierarchy, it _is_ necessary to specify precisely where enums fit into the class hierarchy. | ||
|
||
In practice, the implementation of the enhanced enums will likely be something similar. | ||
|
||
Either first declare `Enum` as: | ||
|
||
```dart | ||
abstract class Enum { | ||
Enum._(this.index, this._name); | ||
final int index; | ||
final String _name; | ||
String _$enumToString(); | ||
String toString() => _$enumToString(); | ||
} | ||
``` | ||
|
||
*or* retain the current `_Enum` class and make that the actual superclass of `enum` classes. Either works, I’ll use `Enum` as the superclass directly in the following. | ||
|
||
Then desugar an `enum` declaration to an actual `class` declaration and rewrite every generative constructor of the `enum` declaration to take two extra leading positional arguments. | ||
An `enum` declaration is desugared to an actual `class` declaration, which extends a private `_Enum` class in `dart:core`. _This class holds the `final int index;` declaration and a `final String _name;` declaration (used by the the `EnumName.name` getter), and both fields are initialized by a constructor._ Every generative constructor of the `enum` declaration is rewritten to take two extra leading positional arguments. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo: "used by the the" |
||
|
||
The `enum` declaration: | ||
|
||
|
@@ -219,7 +203,7 @@ enum LogPriority with LogPriorityMixin implements Comparable<LogPriority> { | |
would then desugar to something like: | ||
|
||
```dart | ||
class LogPriority extends Enum with LogriorityMixin implements Comparable<LogPriority> { | ||
class LogPriority extends _Enum with LogriorityMixin implements Comparable<LogPriority> { | ||
static const warning = LogPriority._$(0, "warning", 2, "Warning"); | ||
static const error = LogPriority._$(1, "error", 1, "Error"); | ||
static const log = LogPriority._$unknown(2, "log", "Log"); | ||
|
@@ -242,8 +226,6 @@ class LogPriority extends Enum with LogriorityMixin implements Comparable<LogPri | |
|
||
where the `_$` represents a fresh name. | ||
|
||
In practice, we may choose to have a subclass of `Enum` as the actual superclass of the `enum` class, rather than use `Enum` directly. We’ll have to make sure that it makes no difference wrt. which declarations are valid (at least outside of `dart:core`, and for `enum`s declared inside `dart:core` it’s our own responsibility to not conflict with names used by the enum implementation.) | ||
|
||
## Summary | ||
|
||
We let `enum` declarations be much more like the classes they are, with the only restriction now being that it’s classes with a fixed number of known constant instances. We allow the class to apply mixins (applicable to a supertype of `Enum`) and implement interfaces. We allow any static or instance member declaration, and any generative `const` constructor declaration (so instance variables must be final, including those added by mixins, otherwise the mixin application constructor forwarders to the superclass `const Enum()` constructor won’t be `const`). | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really wish this wasn't necessary. We're specifying an implementation detail.
Could we at least avoid specifying the name of the class?
Just say that there exists an anonymous class which implements
Enum
and whichenum
classes extend,so that the depth of an
enum
class is at least depth(Enum
) + 2.That still implies that there is a class at depth(
Enum
) + 1 that other classes can conflict with. Can we introduce a third class at depth depth(Enum
) + 1 thatenum
classes also implement, so UP can never yield_Enum
? Like forclass HideEfficientLengthIterable
does forEfficientLengthIterable
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking more about this, I'd prefer to just want to make it unspecified behavior.
Or stop extending
_Enum
. It is an implementation detail.Heck, make
_Enum
not implementEnum
, and it would give it a lower depth. (And make inferringEnum
impossible, because LUB is moronic.) Or make_Enum
a superclass ofEnum
.I really, really do not want to enshrine a completely arbitrary private implementation class in the specification. I'd rather make every tool lie about it. That is a more future proof design.
(I do consider if we can make enum classes be allowed to have superclasses, so they only implement
Enum
. Maybe make_Enum
a mixin then - probably needs more mixin features.)