[NNBD] checking for null of final instance variable doesn’t work

This code should raises an unchecked_use_of_nullable_value error on the marked line. I think, the guarding if should be sufficient because the instance variable is final.

class Context {
  final Context? parent;

  Context(this.parent);

  int lookup(String name) {
    if (parent != null)
      return parent.lookup(name); // unchecked_use_of_nullable_value
    return 0;
  }
}

1 possible answer(s) on “[NNBD] checking for null of final instance variable doesn’t work

  1. Absolutely would be useful to do a check+binding in one operation.

    C# has if (e is Foo y) which binds y to the value of e with static type Foo, after checking that it has that type. I like that syntax, and it should be compatible with Dart.

    No plans right now, though.