Skip to content

unable to infer non nullable type in case of generics #56954

Closed
@bar4488

Description

@bar4488

In these cases we should be able to infer that the type cannot be null, however the current implementation raises a compile error:

class A<T> {
  final T value;

  A(this.value);
}

void main() {
  int? n = 3;
  int n2 = n == null ? 3 : n + 2; // as intended

  if (n != null) {
    int n2 = n; // as intended
  }

  A<int?> a = A(3);
  int b = a.value == null ? 3 : a.value + 2; // error: Operator '+' cannot be called on 'int?' because it is potentially null

  if (a.value != null) {
    int b = a.value; // error: A value of type 'int?' can't be assigned to a variable of type 'int'.
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).closed-as-intendedClosed as the reported issue is expected behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions