7

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.

Fallenreaper
  • 10,222
  • 12
  • 66
  • 129
  • Do you actually want to check if a `Type` instance is a subtype of `num` of if the runtime type of a value is the subtype of `num`? Something like http://stackoverflow.com/questions/31682371 ? – Günter Zöchbauer Feb 01 '16 at 18:38

1 Answers1

8

1. The Answer

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.

2. Additional, Useful Info

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.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • So in my cases of genericism, i would check if `_value is num` instead of `T is num` That sounds good. I just wasnt sure if you could do it with classes as well, or if it was just the instantiated objects. Thank you. – Fallenreaper Feb 01 '16 at 18:45
  • As a followon, if you did something like: `int test;`, you cant test `test` as a num, because while space is allocated, it is not set to anything, returning false; – Fallenreaper Feb 01 '16 at 18:53
  • 1
    With classes you need to use reflection ('dart:mirrors' or reflectable package). – Günter Zöchbauer Feb 01 '16 at 18:56
  • 1
    `is` tests the runtime type. The runtime type of `int test;` is `Null`. `Null` is not a subclass of `num`. – Günter Zöchbauer Feb 01 '16 at 18:57
  • Ok, I may have to use reflection if i cant use the runtime, thank you – Fallenreaper Feb 01 '16 at 19:00
  • @PhilippeFanaro Thanks for the improvement. It's nor clear to me what you mean with "use is on classes" and "before runtime". `is` works with (custom) classes just fine and reflection is as well at runtime. Before runtime would be for example a code generation step where you use features from DartAnalyzer to get such information about existing source code. – Günter Zöchbauer Apr 24 '20 at 03:06
  • @GünterZöchbauer, I think I misunderstood that. It would be better if you fixed it, as you seem more competent at it. What I had in mind was a statement like `CustomA is A`, which I thought should give me `true` if `CustomA` either implemented or inherited `A`. As you can see, I haven't yet mastered this part of Dart yet. Anyway, improve/correct my edit as you see fit. – Philippe Fanaro Apr 24 '20 at 12:12
  • @PhilippeFanaro no worries. "should give me `true`" - does it not? It's still not clear what you mean with "before runtime". – Günter Zöchbauer Apr 24 '20 at 13:08
  • 1
    @GünterZöchbauer that whole paragraph is just probably completely mistaken. What I was thinking about was that checking something like `CustomA is A` would be done at compile time because `CustomA` and `A` are simply classes or types. But `is` is a runtime check, so that shouldn't make sense (checks like this are runtime only anyway...); that's what you get when you write stuff at midnight :). Anyway, take a look at [this Dartpad example](https://dartpad.dev/aa6fffc4b467e08eb06746ccc0cf8850), does that make sense in Dart? – Philippe Fanaro Apr 24 '20 at 13:32
  • @PhilippeFanaro Sorry, it was my fault. I didn't properly read what the question is about. – Günter Zöchbauer Apr 24 '20 at 16:15