-
Notifications
You must be signed in to change notification settings - Fork 213
extension enum #4329
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
Comments
With primary constructors you can write enum SudoEnum(int i) { option1(1), option2(2), option3(3) } EDIT: for reference see #2364. |
Maybe I am strange, but I somehow care about creating an extra class, and that is why I use extension types. |
Nothing prevents you from doing Trying to prevent that cast won't work, there'll always be a way, as long as it's an extension type. |
And to be clear: You are strange. So am I. I completely get not wanting ... well, not the class, but the instance of the class. The language just doesn't support that. The things you want from an enum are things that are inherently based on controlling identity of the values, and extension types delegate the identity, and control of instance creation, to the representation type. The feature you want is not in the extension type feature. |
The thing I want is just some integer that cannot be miswritten in code in wrong place. The function or variable should not use the wrong integer, and the integer sould not be used in wrong way, so use extension type. But simply creating a value manually everywhere is definitely a bad idea since I can remember or mistyped wrong value, so they should be predeclared in right scope with a readable name with meaning. Declaring those integer is wordy. Enum looks simple, but why should I create a runtime class that exist just for a simple option? I am just finding some easier way to declare all these static const. |
As a convenience and help from the type system in not using incorrect values, an extension type with a fixed set of constants will work perfectly fine. But the enum value syntax is awesome. extension type Foo(Bar _) {
enum foo1(bar1), foo2(bar2);
} It'll just be shorthand for Could be used in plain classes and mixins too. |
It's an interesting idea, but I think using the But if there was such a feature, I would expect there to be no keyword, just a requirement that it come before any other member (same restriction enums currently have), and that it can only be used with extension type const Foo(Bar _) {
foo1(bar1), foo2(bar2); // must come before other members and
// only allowed with const constructors
} Alternatively, extension type const Foo(Bar _) {
static const foo1(bar1), foo2(bar2);
// static const foo1 = Foo(bar1), foo2 = Foo(bar2);
}
class Baz {
Baz(this.x, this.y);
Baz.named(this.x, this.y);
int x, y;
static Baz a(1, 2), b.named(3, 4);
// static Baz a = Baz(1, 2), b = Baz.named(3, 4);
static final c(5, 6), d(7, 8);
// static final c = Baz(5, 6), d = Baz(7, 8);
} |
Nested enum declarations would conflict. Sadly a good point, so just the Mixins, abstract and sealed classes could work with const factory constructors (if we haven't allowed mixins to have factory constructors yet, we should). Just Should just make it look like markdown or yaml 😁 class const Foo(int x) {
- va(1)
- vb(2)
- vc(3)
;
}
Not very "Dart-ish", though. (And should |
Fair enough regarding abstract and sealed classes, but mixins can not even declare a factory constructor. Edit: I should note that factory constructors are not allowed in enums, so It's unclear if it's entirely necessary for such a feature to support factory constructors.
I'm not sure you would need a different syntax altogether to disambiguate.
const bar = 1;
abstract class Foo {
foo(bar),; // trailing comma required because
// ambiguous with abstract method
// alternatively, these should be unambiguous also
// foo(1);
// foo.new(bar);
const Foo._(this.value);
const factory Foo(int value) = _Foo;
final int value;
}
class _Foo extends Foo {
const _Foo(super.value) : super._();
} const bar = 1;
class Foo {
foo(bar); // no trailing comma required because
// concrete classes cannot have abstract methods
const Foo(this.value);
final int value;
} |
When I need a set options(typically for switch), no run-time type check required, enum in dart is an over-kill, I would use extension type and static const variables instead.
Just like using the same syntax in a class, it is verbose, therefore I expect a feature similar to enum as extension type. Though not having it would be fine, having it would be great.
This is how it would look like right now
And this is what I expect it to be
The text was updated successfully, but these errors were encountered: