Simple cases of, int being a number really.
Im creating a generic class test<T>
and i want to test T to see if its inherited from number (or num in the case of Dart).
Based on the Objects, Object > num > int
in the docs, so at first I was thinking that: T is num
would work if T
is int
. Testing int is num
shows false. There must be another keyword i need to use instead of is
to determine if it is a child of a particular class.
Im trying to set it up such that if T is a child of num it will process comparisons differently than if they were strings.
To me, this goes back to the polymorphism-inheritance design of is-a and has-a relationshis.
With int is num
you test if an instance of Type
is a subtype of num
which is correctly reported as false
.
What you want to test is rather like 5 is num
.
Try it in DartPad.
As noted in the comments, it's also useful to note that is
tests for the runtime type. So, if you're working with a non-initialized variable — e.g., int nonInitializedVar;
—, with runtime type of Null
, Dart will return false
to the is
test, because Null
is not a subclass of num
.
Another important point is that if you want to use is
on types (classes) at runtime you will need to use reflection, with packages such as dart:mirrors
or reflectable
or the dart-analyzer to do it at build time for code generation.