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

Specify that enums extend _Enum from dart:core. #3671

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 3 additions & 21 deletions accepted/2.17/enhanced-enums/feature-specification.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Member

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 which enum 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 that enum classes also implement, so UP can never yield _Enum? Like for class HideEfficientLengthIterable does for EfficientLengthIterable.

Copy link
Member

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 implement Enum, and it would give it a lower depth. (And make inferring Enum impossible, because LUB is moronic.) Or make _Enum a superclass of Enum.

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.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: "used by the the"


The `enum` declaration:

Expand All @@ -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");
Expand All @@ -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`).
Expand Down