Tom C
Tom C

Reputation: 842

Why are types defined in type parameters when extending an object different to when referenced in the main function?

Hard to explain in words however I have the following code:

void main() {
  Test2 test2 = new Test2();

  // This one is different?
  printHashcode(test2.type);

  // Following 2 are the same
  printHashcode(Test2);

  printHashcode(Test2);

}
void printHashcode(Type t) {
  print("$t hashcode: ${t.hashCode}");
}

class Test<T> {
  Type type = T;
}


class Test2 extends Test<Test2> {
  Test2 () {

  }
}

Example output:

Test2 hashcode: 629420109

Test2 hashcode: 229136709

Test2 hashcode: 229136709

The output I would expect is the 3 prints in the main function to be the same however the first one is different. Is this a bug or an intended feature?

If its an intended feature, is there a way of getting the same Type object as in the definition of the Test2 class?

Thanks,

Upvotes: 1

Views: 85

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657781

I just tried your code and I get the same output for all three print statements

Test2 hashcode: 683363706
Test2 hashcode: 683363706
Test2 hashcode: 683363706

Just updated to latest development release (raw channel)

Dart VM version: 1.1.0-dev.4.0 (Fri Dec 13 03:22:00 2013) on "linux_x64"

Upvotes: 1

Related Questions