Man Hoang
2015-09-16 15:27:50 UTC
As far as I can see, there are two big limitations of enum at the moment.
1. No easy way to know if an object is an enum (of and kind)
var isEnum = (value ! null) && reflectClass(value.runtimeType).isEnum;
// In C#, it's so easy
// var isEnum = value is Enum;
2. No built-in support for parsing an integer or a string into an enum
value. This is how I implement my own parsing method.
/// Converts [input], which is an integer or a string, into an enum
value of
/// type [enumType].
/// Returns `null` if fails.
parseEnum(input, Type enumType) {
try {
var values = reflectClass(enumType).getField(#values).reflectee;
if (input is int) {
return values[input];
}
for (var value in values) {
if (value.toString().split('.')[1] == input) {
return value;
}
}
} catch (e) {
}
return null;
}
So, will the Dart team resolve these limitations in the next version of the
language?
1. No easy way to know if an object is an enum (of and kind)
var isEnum = (value ! null) && reflectClass(value.runtimeType).isEnum;
// In C#, it's so easy
// var isEnum = value is Enum;
2. No built-in support for parsing an integer or a string into an enum
value. This is how I implement my own parsing method.
/// Converts [input], which is an integer or a string, into an enum
value of
/// type [enumType].
/// Returns `null` if fails.
parseEnum(input, Type enumType) {
try {
var values = reflectClass(enumType).getField(#values).reflectee;
if (input is int) {
return values[input];
}
for (var value in values) {
if (value.toString().split('.')[1] == input) {
return value;
}
}
} catch (e) {
}
return null;
}
So, will the Dart team resolve these limitations in the next version of the
language?
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.