Jerome Puttemans
Jerome Puttemans

Reputation: 1113

Dart 2.7 generic extends

I'm facing a problem converting dart 1.25 code to 2.7. My problem is with generic extends contraint.

With the older dart 1.25 version the generic type was understood to num when it wasn't specified.

void main() {
  CustomType customType = new CustomType();// no T specified
}

class CustomType<T extends num> {
  CustomType() {
    print(T is num);//> why is this false ?
  }
}

Why isn't the case ?

Upvotes: 0

Views: 85

Answers (1)

Lesiak
Lesiak

Reputation: 25966

You used a wrong operator to compare classes. Use:

  • == to compare 2 classes for equality
  • is to check if an object is an instance of a class.
void main() {
  CustomType customType = new CustomType();// no T specified
}

class CustomType<T extends num> {
  CustomType() {
    print(T);        // num
    print(T == num); // true
    print(1 is num); // true
  }
}

Upvotes: 2

Related Questions