-
Notifications
You must be signed in to change notification settings - Fork 195
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
Enums, value classes and beyond #183
Comments
Is there a reason why Dart
|
The currently-being-worked-on enhanced enum DataType {
text._(7),
image._(8);
final int value;
const DataType._(this.value);
factory DataType(int value) {
for (var e in values) {
if (e.value == value) return e;
}
// You can't return `null` from a constructor in null safe code.
throw ArgumentError.value(value, "value", "Not the value of a DataType");
}
@override
String toString() => '$value ($name)';
} |
Hi. These topics have been brought up several times in the language and SDK repos, however I'd like to show a particular use case that may help with designing future language features.
While writing parsers or generators for binary file formats, the following code evolution usually happens. Out of simplicity, this is how it starts:
Then, either the number of consts grows, or another field emerges. So, to keep values organized, static consts within abstract classes could be used. This approach scopes values (thus preventing certain errors), but doesn't provide full type safety, since values are just integers.
To achieve type safety, much more boilerplate is needed:
The next step usually involves adding string names to these values and overriding
toString
for a better debugging.Could future language development reduce the amount of boilerplate here or it would be more appropriate to go with codegen?
The text was updated successfully, but these errors were encountered: