dartenumsextendrestrict

Restrict or extend an enum in dart


I'm working with dart and let's say I have an enumeration (potentially really long) that I cannot modify:

enum Animal {
  cat,
  dog,
  lion,
  tigger,
}

Is it possible to restrict this enum, I would like to obtain

enum DomesticAnimal {
  cat,
  dog,
}

with DomesticAnimal.cat still being an Animal ?


Also, is it possible to extend it to obtain

enum LivingCreature {
  cat,
  dog,
  lion,
  tigger,
  tree,
  flower,
}

where LivingCreature.cat still being an Animal ?


Solution

  • No. There is currently nothing like that in Dart. An enum value object belongs to exactly one enum type. There is also no way to create new type which contains just some instances of other types.