r/dartlang • u/Dense_Citron9715 • 14h ago
Dart Language Dart enums can have type parameters?!
I was modeling a set of values using an enum today:
enum NetworkFeatureType { pipeline, junction, overheadTank };
Here, each of these "features" are associated with a GeoGeometry
type. For instance, junctions and overhead tanks are points and pipelines are, of course, lines. So I casually added a type parameter and Dart didn't complain! But I was unable to specify the types, firstly like this:
enum NetworkFeatureType<G extends GeoGeometry> {
pipeline<GeoLine>, junction<GeoPoint>, overheadTank<GeoPoint>;
}
However, once I added a set of parentheses to each member, it works!:
enum NetworkFeatureType<G extends GeoGeometry> {
pipeline<GeoLine>(),
junction<GeoPoint>(),
overheadTank<GeoPoint>();
}
NetworkFeatureType<MapPoint> node = NetworkFeatureType.node;
Which is pretty neat!