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
- [BUG] Cowork (Windows): all Plan9 shares fail with "Plan9 mount failed: invalid argument" after Windows update KB5124008 (26200.9445) — uninstalling the KB fixe
- Feature Request: Auto-resume CLI session when usage limit resets
- Please deobfuscate the Python
- [Bug] MLX version of Gemma 4 26B fails to load: "Model type gemma4 not supported" (macOS)
- # 233 Developers Discussed Abandoning the Office Add-in Platform — An Open Letter on the Office.js Stability, Security & Trust Crisis
- [FEATURE] Allow users to configure which shell the Bash tool uses or inherit initial shell
Raise real-world problems, find people facing the same issue, and discuss solutions.3,000 issues0 comments
reliability (Software)
[BUG] Cowork (Windows): all Plan9 shares fail with "Plan9 mount failed: invalid argument" after Windows update KB5124008 (26200.9445) — uninstalling the KB fixe
Feature Request: Auto-resume CLI session when usage limit resets
Please deobfuscate the Python
[Bug] MLX version of Gemma 4 26B fails to load: "Model type gemma4 not supported" (macOS)
# 233 Developers Discussed Abandoning the Office Add-in Platform — An Open Letter on the Office.js Stability, Security & Trust Crisis
[FEATURE] Allow users to configure which shell the Bash tool uses or inherit initial shell
Admin Materials and Surfaces
AMD Support
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
Comments (0)
No comments yet. Start the discussion.