Is there a way to constrain a generic type to be an enum, something like the following?

class MyClass<T extends enum> {}

Something like this in C#.


Solution 1: JCKödel

It is not possible in Dart. I had the same issue converting enum properties to a SQLite database (which can hold only its numeric types), so I needed enum.values to "parse" the integer to enum and enum.index to convert the enum value to an int.

The only way possible is to cast the enum to dynamic or passing the enum values.

Example:

T mapToEnum<T>(List<T> values, int value) {
  if (value == null) {
    return null;
  }

  return values[value];
}

dynamic enumToMap<T>(List<T> values, T value) {
  if (value == null) {
    return null;
  }

  return values.firstWhere((v) => v == value);
}

So I can use like this:

final SomeEnum value = SomeEnum.someValue;
final int intValue = enumToMap(SomeEnum.values, value);
final SomeEnum enumValue = mapToEnum(SomeEnum.values, value.index);


Solution 2: xbalaj

It is now possible to constrain a generic type to an Enum since Dart 2.16.

You can do so in a following way:

class MyClass<T extends Enum> {}

Now you can pass to the generic parameter T of MyClass only enum.


Solution 3: Dinesh

Adding some extra context to @xbalaj answer. This is how you can pass enums as generics.


enum Fruits {
 apple,
 orange,
 banana
}

enum Numbers {
 one,
 two,
 three
}

void main() {
   print("Numbers:");
   var numberEnums = MyClass<Numbers>(Numbers.values); 
   print("Fruits:");
   var fruitEnums = MyClass<Fruits>(Fruits.values);
}

class MyClass<T extends Enum> {
  List<T> myEnum;

  MyClass(this.myEnum) {
   print(myEnums);

   for (final e in myEnums) {
     if (e is Numbers) {
      // Do something specific to numbers
     } 

     if (e is Fruits) {
     // Do something specific to fruits
     }
   }

  }

}

Output:

Numbers: 
[Numbers.one, Numbers.two, Numbers.three]
Fruits:
[Fruits.Fapple, Fruits.orange, Fruits.banana]