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

[3.2] Private field promotion docs #4254

Closed
8 tasks done
MaryaBelanger opened this issue Oct 3, 2022 · 4 comments · Fixed by #5237
Closed
8 tasks done

[3.2] Private field promotion docs #4254

MaryaBelanger opened this issue Oct 3, 2022 · 4 comments · Fixed by #5237
Assignees
Labels
a.effective-dart Relates to the best practices explained in Effective Dart dev.type-promotion Relates to type promotion concepts and examples e2-days Can complete in < 5 days of normal, not dedicated, work from.team Reported by Dash docs team member p2-medium Necessary but not urgent concern. Resolve when possible. st.triage.ltw Indicates Lead Tech Writer has triaged
Milestone

Comments

@MaryaBelanger
Copy link
Contributor

MaryaBelanger commented Oct 3, 2022

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 and final (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. A private field tells the compiler that an implementing class from another library isn't able to change the value.

So private and final 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

References

@parlough parlough added a.effective-dart Relates to the best practices explained in Effective Dart p2-medium Necessary but not urgent concern. Resolve when possible. e1-hours Can complete in < 8 hours of normal, not dedicated, work e2-days Can complete in < 5 days of normal, not dedicated, work meta.evaluation Need to consider what changes if any are needed labels Oct 3, 2022
@stereotype441
Copy link
Member

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).

@parlough parlough added this to the Next stable release milestone Oct 3, 2022
@MaryaBelanger MaryaBelanger self-assigned this Oct 5, 2022
@MaryaBelanger MaryaBelanger added on-hold st.WIP Issue in progress and removed meta.evaluation Need to consider what changes if any are needed on-hold labels Oct 12, 2022
@MaryaBelanger
Copy link
Contributor Author

Note: need to take into consideration record fields as well

@MaryaBelanger MaryaBelanger changed the title [2.19] Private field promotion docs Private field promotion docs Nov 22, 2022
@atsansone atsansone added the st.triage.ltw Indicates Lead Tech Writer has triaged label Apr 29, 2023
@atsansone atsansone added the dev.type-promotion Relates to type promotion concepts and examples label Jun 9, 2023
@parlough parlough modified the milestones: Next Major Release, Dart 3.2 Aug 4, 2023
@parlough parlough changed the title Private field promotion docs [3.2] Private field promotion docs Aug 4, 2023
@atsansone atsansone added the from.team Reported by Dash docs team member label Aug 8, 2023
@parlough parlough removed e1-hours Can complete in < 8 hours of normal, not dedicated, work st.WIP Issue in progress labels Aug 30, 2023
@parlough
Copy link
Member

parlough commented Oct 10, 2023

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 parlough reopened this Oct 10, 2023
@MaryaBelanger
Copy link
Contributor Author

@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 :)

parlough pushed a commit that referenced this issue Nov 15, 2023
…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?
atsansone pushed a commit to atsansone/site-www that referenced this issue Jan 26, 2024
…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?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a.effective-dart Relates to the best practices explained in Effective Dart dev.type-promotion Relates to type promotion concepts and examples e2-days Can complete in < 5 days of normal, not dedicated, work from.team Reported by Dash docs team member p2-medium Necessary but not urgent concern. Resolve when possible. st.triage.ltw Indicates Lead Tech Writer has triaged
Projects
None yet
4 participants