Dart perform runtime subtype checking
Solution 1:
If all you want is to chcek the subtype relation of two types, you don't need arguments at all, only type arguments.
bool isSubtype<S, T>() => <S>[] is List<T>;
You also can't extract the runtime type of an object as a type argument.
I'm guessing that's what you are trying to do with the arguments and type inference, but type inference happens at compile-time and only uses the static types of the arguments.
The call print(isSubType(type, a))
infers the type List<dynamic>
for both type arguments from the expressions type
and a
, and does so at compile-time and independently of the actual run-time type that variable will contain.
There is no general way, outside of dart:mirrors
, to check whether two objects have runtime types that are related to each other, because it's not possible to get the runtime type of an object into a type variable.
You also can't do anything similar with Type
objects. A Type
object is only really good for passing into dart:mirrors
, and maybe compare for equality. It doesn't understand subtyping at all.