-
Notifications
You must be signed in to change notification settings - Fork 683
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
[3.2] Private field promotion docs #4254
Comments
Minor nit: when you say "and getters of the same name, in the same library, are also final" I think you mean something like "and there are no concrete getters of the same name, in the same library". (There's no such thing as a final or non-final getter). |
Note: need to take into consideration record fields as well |
I don't think you intended to close this as done yet. I'm going to reopen it, but feel free to close if that's what you meant :) |
@parlough Thanks, that wasn't supposed to happen! 😱 I think it happened because I linked the recent go-link pr to this, and just closed that. But you're right, definitely not anywhere near being done :) |
…promotion (#5325) --- Part of #4254 --- Questions / notes: I'm not sure if the wording in these parts need to be slightly adjusted, or are unrelated. - Line 405: > **Top level variable and static field declarations must have an initializer.** Since these can be accessed and assigned from anywhere in the program, it’s impossible for the compiler to guarantee that the variable has been given a value before it gets used. The only safe option is to require the declaration itself to have an initializing expression that produces a value of the right type: - not sure if this statement is affected, because later is says that local variables are the most flexible case, I'm assuming because they can be type promoted? So are the rules about fields now also more flexible, at least for private final fields, since they can be promoted now too? - [Late variables](https://dart.dev/null-safety/understanding-null-safety#late-variables) - "The most common place where the type checker cannot prove the safety of code is around top-level variables and fields." ... technically still true, maybe adjust?
…promotion (dart-lang#5325) --- Part of dart-lang#4254 --- Questions / notes: I'm not sure if the wording in these parts need to be slightly adjusted, or are unrelated. - Line 405: > **Top level variable and static field declarations must have an initializer.** Since these can be accessed and assigned from anywhere in the program, it’s impossible for the compiler to guarantee that the variable has been given a value before it gets used. The only safe option is to require the declaration itself to have an initializing expression that produces a value of the right type: - not sure if this statement is affected, because later is says that local variables are the most flexible case, I'm assuming because they can be type promoted? So are the rules about fields now also more flexible, at least for private final fields, since they can be promoted now too? - [Late variables](https://dart.dev/null-safety/understanding-null-safety#late-variables) - "The most common place where the type checker cannot prove the safety of code is around top-level variables and fields." ... technically still true, maybe adjust?
This is an evaluation ticket
Evaluating the possible need for doc updates due to dart-lang/language#2020
Summary
Type promotion is currently only available for local variables. #2020 will make it possible for class fields to be safely promoted if they meet certain criteria: when the field is
private
andfinal
(and there are no concrete getters of the same name, in the same library).Limiting promotable fields to
final
tells the compiler the instance value can't change (and therefore, type promotion remains sound). But,final
doesn't protect soundness if another class implements the original class and overrides its field declarations. So, the compiler scans the library for implementing classes and can prevent type promotion of the original because of it.But what if a class outside of the library implements the defining class and overrides its fields? The compiler can't check for implementing classes outside of the original's library, which is why the field must also be
private
. Aprivate
field tells the compiler that an implementing class from another library isn't able to change the value.So
private
andfinal
allow the compiler to soundly promote a type because they inherently mean the value cannot be overridden outside of the compiler's purview.One loophole (
nosuchmethod
overriding interfaces in an implementing class that doesn't fulfill the interface contract, i.e. doesn't explicitly declare the fields of the original class) is covered in #49687 and another evaluation ticket (#4263).Evaluation
Fixing type promotion failures
[3.2] Update "Fixing type promotion failures" for private final field promo #5246
The sections on this page are actually linked to in type promotion error messages.
Only local variables can be promoted is no longer an error
"Write captured by a local function", "Written outside of the current closure or function expression", "Write captured outside of the current closure or function expression" sections all have "create a local variable" as a solution / workaround, so I'm wondering if they'll still be errors once non-local variables can be promoted, or maybe the workaround will change to utilize private final field promotion
New sections will need to be added to support new error messages from 2020 (@stereotype441 evaluating actual error code):
Effective Dart
Implementation draft here:
Effective Dart: update type promotion suggestion for private final #4272New: [3.2] Update effective dart mention of "only local variables can be promoted" #5324
The entire workaround is technically no longer necessary, maybe the section can be removed?Should we add a "CONSIDER" section recommending declaring private final fields if you want type promotion to happen?The Dart type system
Fixing common type problems is another too-similar page that might need to be addressed to not detract from the source of truth that Fixing type promotion failures should be.
Overview (nothing related)
FAQ (nothing related)
Codelab
Understanding null safety:
[3.2] Understanding null safety page updates for private final field promotion #5325
References
The text was updated successfully, but these errors were encountered: