-
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
enum
s extending other types
#3780
Comments
It's not impossible. We currently make Then we allow mixins on top of If instead we allowed extending any class, and instead introduced the We could just introduce the fields in each We could perhaps introduce a hack that allowed initializing the mixin, without adding it as a general language feature. So unless the priority of this request increases, I'll expect it to be blocked on adding constructors to mixins. If we have that, it becomes a much smaller feature to allow superclasses for |
@lrhn thanks very much for the reply.
I'm wondering if it's possible to implement enums using a macro rather than a mixin. I don't really understand how the abstract interface class Enum {
int get index;
String get _name;
}
abstract class _Enum implements Enum {
@override
final int index;
@override
final String _name;
const _Enum(this.index, this._name);
@override
String toString() => '$runtimeType.$_name';
} And when you declare an enhanced enum enum MyEnum {
a(true),
b(false),
c(null);
const MyEnum(this.value);
final bool? value;
} it's interpreted as follows: class MyEnum extends _Enum {
static const a = MyEnum(0, 'a', true);
static const b = MyEnum(1, 'b', false);
static const c = MyEnum(2, 'c', null);
static const values = [a, b, c];
const MyEnum(super.index, super._name, this.value);
final bool? value;
} What if we got rid of @EnumMacro()
class MyEnum {
static const a = MyEnum(true);
static const b = MyEnum(false);
static const c = MyEnum(null);
static const values = [a, b, c];
const MyEnum(this.value);
final bool? value;
} And the augment class MyEnum implements Enum {
static const a = MyEnum(0, 'a', true);
static const b = MyEnum(1, 'b', false);
static const c = MyEnum(2, 'c', null);
static const values = [a, b, c];
const MyEnum(this.index, this._name, this.value);
final bool? value;
@override
final int index;
@override
final String _name;
@override
String toString() => '$runtimeType.$_name';
} It seems to me like this could work without a need for mixin constructors, but there's a good chance that I'm misunderstanding some part of it. |
Using a macro, or just generating the fields directly on the
The second point affects the code size of web compiles, probably all AoT compilers. Using the same private field for everything also keeps the code monomorphic. |
Sounds good, thanks for the info! |
Combined mixins would address this issue as well.. The current work around for effectively extending an Enum type is either, extension methods on an Enum subtype, or It can be left up to the user to essentially split implementation into 2 parts, a mixin for type constraint, and mixin[s] for methods implementations.
layered/multiple inheritance is reflected in the with clause, although type constrained for correctness, can become cumbersome. |
Proposal
Allow the following as valid dart code:
My understanding is that the
enum
keyword desugars to something likewhen instead, it could be
With this change, you could make enumerated collections for widgets, colors, decorations, text styles… anything with a
const
constructor!before
after
I'd love to see this implemented as part of the code generation release.
The text was updated successfully, but these errors were encountered: