IssueRaiser

If-variables

*This is a proposal for how to handle the lack of field promotion with null safety (though it covers more than just that).* The big hammer in the language for making nullable types usable is flow analysis and type promotion. This lets the imperative code that users naturally write also seamlessly and soundly move nullable variables over to the non-nullable type where the value can be used. Unfortunately, this analysis isn't sound for fields and getters, so those do not promote: ```dart class C { Object obj; test() { if (obj is int) obj + 1; // Error. :( } } ``` One option is to [enable promotion in the cases where using the field *is* sound](https://github.com/dart-lang/language/issues/1167), but the boundary there is subtle, it's easy to move a variable across it, and may be too narrow to cover most cases. Another option is to automatically [promote fields when failure to do so would cause a static error](https://github.com/dart-lang/language/issues/1188). That trades static failures, which let users *know* their code is unsound, with a runtime error that could cause their program to crash. Given that the trend in Dart is *away* from code that may *silently* fail at runtime, I'm not enthusiastic about the latter approach. This proposal describes a feature called **"if-variables"** that is local, sound, efficient, explicitly opted in (while being concise), cannot fail at runtime, and covers a larger set of painful cases than any of the other proposals. It look

Posted by munificent on Sep 2, 2020, 10:41 PM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.3,000 issues0 comments

reliability (Software)

If-variables

munificentSep 2, 2020, 10:41 PM

*This is a proposal for how to handle the lack of field promotion with null safety (though it covers more than just that).* The big hammer in the language for making nullable types usable is flow analysis and type promotion. This lets the imperative code that users naturally write also seamlessly and soundly move nullable variables over to the non-nullable type where the value can be used. Unfortunately, this analysis isn't sound for fields and getters, so those do not promote: ```dart class C { Object obj; test() { if (obj is int) obj + 1; // Error. :( } } ``` One option is to [enable promotion in the cases where using the field *is* sound](https://github.com/dart-lang/language/issues/1167), but the boundary there is subtle, it's easy to move a variable across it, and may be too narrow to cover most cases. Another option is to automatically [promote fields when failure to do so would cause a static error](https://github.com/dart-lang/language/issues/1188). That trades static failures, which let users *know* their code is unsound, with a runtime error that could cause their program to crash. Given that the trend in Dart is *away* from code that may *silently* fail at runtime, I'm not enthusiastic about the latter approach. This proposal describes a feature called **"if-variables"** that is local, sound, efficient, explicitly opted in (while being concise), cannot fail at runtime, and covers a larger set of painful cases than any of the other proposals. It look

Comments (0)

No comments yet. Start the discussion.

Comment on this issue