Skip to content

Inference of type bounds in as checks #4366

Open
@dcharkes

Description

@dcharkes

Would it be possible to infer type bounds in as checks?

Consider the following example:

class A<T extends Object> {
  Object? get value => null;
}

class B<T extends Object> extends A<T> {
  @override
  final T value;

  B(this.value);
}

void example() {
  final A<int> a = B<int>(123);
  if (a is B) { // inferred as `a is B<Object>`. It would be nice if this would be inferred as B<int>
    final x = a.value; // inferred as `final Object? x` due to `a` not being promoted to `B<int>` and staying `A<int>`. 
  }
  if (a is B<int>) { // manually specifying `B<int>` promotes `a` to `B<int>` from `A<int>`
    final x = a.value; // which means `x` is inferred as `int`
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureProposed language feature that solves one or more problems

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions