I recommend avoiding using Type
objects for anything serious (other than printing or dart:mirrors
).
You can create functions to check whether two types, provided as type arguments, are equivalent. Here are some examples:
/// Whether two types are equivalent.
///
/// The types are equivalent if they are mutual subtypes.
bool equivalentTypes<S, T>() {
return _Helper<S Function(S)>() is _Helper<T Function(T)>;
}
class _Helper<T> {}
// Or alternatively:
bool equivalentTypes2<S, T>() {
S func(S value) => value;
return func is T Function(T);
}
/// Whether two types are the same type.
///
/// Uses the same definition as the language specification for when
/// two types are the same.
/// Currently the same as mutual subtyping.
bool sameTypes<S, T>() {
void func<X extends S>() {}
// Spec says this is only true if S and T are "the same type".
return func is void Function<X extends T>();
}
void main() {
print(equivalentTypes<int, int>());
print(equivalentTypes<int?, int?>());
print(equivalentTypes<int?, int>());
print(equivalentTypes2<int, int>());
print(equivalentTypes2<int?, int?>());
print(equivalentTypes2<int?, int>());
print(sameTypes<int, int>());
print(sameTypes<int?, int?>());
print(sameTypes<int?, int>());
}
The language only has one operator for comparing a type to anything, the is
operator, which compares an object to a type. That's why all the functions here create an object of a know type depending on S
and check it against a type depending on T
.
1 is T
andnull is T
can tell you ifT
isint
,int?
or something else. – Conditional1 is T
and1.0 is T
ordouble.maxFinite is T
, all of them aretrue
forFoo<int?>
orFoo<double?>
. – Eyre1.5 is T
isn't true for bothint?
anddouble?
(and your cases are all true only for Dart for the web, not for the Dart VM). I've added an answer with a more concrete example. – Conditional