I have a map of strings to types in Dart eg animals = <String, Type>{'cat':CatType, 'dog':DogType}
which I modify at runtime. Is there a way to ensure that types added to this map implement a particular interface, either at compile-time or runtime? Something like
animals = <String, Type<? implements AnimalType>>{...}
or during runtime,
void register(name, type):
if type implements AnimalType:
animals[name] = type
No, not really.
The Type
class isn't generic, so you can't have a constrain on that.
You can use the dart:mirrors
library to check whether a Type
object represents a class that implements another type, but it's most likely not worth the effort.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.