Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can class variables explicitly checked for null be assumed non-null? #1472

Closed
tarobins opened this issue Feb 24, 2021 · 1 comment
Closed
Labels
feature Proposed language feature that solves one or more problems state-duplicate This issue or pull request already exists

Comments

@tarobins
Copy link

tarobins commented Feb 24, 2021

I testing the following class in dartpad using Dart SDK 2.12.0-51.0.dev

class Coffee {
  
  List<String>? types;
  
  Coffee(this.types);
  
  void printType() {
    if (types != null) {
      print(types[0]);
    }
  }
}

The compiler complains that types may be null in the print statement.

"An expression whose value can be 'null' must be null-checked before it can be dereferenced - line 16"

I realize the docs say

" The analyzer can’t model the flow of your whole application, so it can’t predict the values of global variables or class fields."

but I wonder in the case were we explicitly check if a class var is not null is supposed to be or could be handled?

@tarobins tarobins added the feature Proposed language feature that solves one or more problems label Feb 24, 2021
@lrhn
Copy link
Member

lrhn commented Feb 24, 2021

See #104 (and follow-up #1187, and maybe a number of the issues linked to those two).

It's not something which is easily fixable in a sound way because we can't know locally whether the variable changes value between the check and the use.
Dart's type promotion works by allowing you to check something about the value of a local variable, and then later use that knowledge when you use the same local variable again. It only works where we can ensure that the variable doesn't lose that property (because its value changes) between the check and use.

For non-local variables, we either can't know that the value doesn't change, or, if we can know it, it's because we can see implementation details that we don't otherwise require the author to not change (making it a breaking change to change, e.g., a final static variable to a static getter).

There has been a number of proposals for either allowing unsound promotion (if you check that the variable isn't null, and then use it as non-null later, we assume you know what you are doing and insert an implicit ! cast for you) or introduce features which makes it easier to create a local variable from a non-local variable. So, we're looking at the issue, but we do not have a good solution which allows type promotion like this.

@lrhn lrhn closed this as completed Feb 24, 2021
@lrhn lrhn added the state-duplicate This issue or pull request already exists label Feb 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Proposed language feature that solves one or more problems state-duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants