Skip to content

Null safety feedback: final field check for null does not convince the compiler field is not null #44100

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

Closed
renatoathaydes opened this issue Nov 6, 2020 · 3 comments
Labels
closed-as-intended Closed as the reported issue is expected behavior

Comments

@renatoathaydes
Copy link

renatoathaydes commented Nov 6, 2020

This fails to compile on line 18, but it should work because the field is final so the null check ensures the field can't be null.

class Hello {
    final String? stackTraceString;
  
    Hello({this.stackTraceString});

    StackTrace? get stacktrace =>
      stackTraceString != null ? StackTrace.fromString(stackTraceString) : null;

}

DartPad Link.

Dart version:

Dart SDK version: 2.11.0-213.5.beta (beta) (Thu Oct 29 11:45:16 2020 +0100) on "macos_x64"
@renatoathaydes renatoathaydes changed the title Null safety feedback: [issue summary] Null safety feedback: final field check for null does not convinces the compiler field is not null Nov 6, 2020
@renatoathaydes renatoathaydes changed the title Null safety feedback: final field check for null does not convinces the compiler field is not null Null safety feedback: final field check for null does not convince the compiler field is not null Nov 6, 2020
@mraleph
Copy link
Member

mraleph commented Nov 7, 2020

That's because final field is not necessarily field at all:

class Hello {
    final String? stackTraceString;
  
    Hello({this.stackTraceString});

    StackTrace? get stacktrace =>
      stackTraceString != null ? StackTrace.fromString(stackTraceString) : null;

}

class MyHello extends Hello {
    @override
	String? get stackTraceString => random.nextBool() ? null : "something";
}

Meaning that you can't soundly promote fields based on a comparison. See dart-lang/language#104 and other linked issues for discussion.

@mraleph mraleph closed this as completed Nov 7, 2020
@mraleph mraleph added the closed-as-intended Closed as the reported issue is expected behavior label Nov 7, 2020
@renatoathaydes
Copy link
Author

Why even have final if the language does not enforce anything one would expect from final?

@mraleph
Copy link
Member

mraleph commented Nov 9, 2020

Why even have final if the language does not enforce anything one would expect from final?

final means you can't write into it. And indeed you can't.

Remember Dart 1 was a very different language from Dart 2. It had type annotations that did not enforce anything - but existed to communicate intent. Some of the design decisions come from that era and unfortunately we will have to live with them for some time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-as-intended Closed as the reported issue is expected behavior
Projects
None yet
Development

No branches or pull requests

2 participants