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

Field-backed properties: readonly backing field #74922

Merged
merged 3 commits into from
Aug 29, 2024

Conversation

cston
Copy link
Member

@cston cston commented Aug 27, 2024

From proposal:

The synthesized backing field is read-only when the containing type is a struct and the property or containing type is declared readonly.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Aug 27, 2024
@cston cston marked this pull request as ready for review August 28, 2024 19:12
@cston cston requested a review from a team as a code owner August 28, 2024 19:12
}
// - The property has no set accessor (but may have an init accessor) and
// the get accessor, if any, is automatically implemented.
else if ((!hasSetAccessor || isInitOnly) && (!hasGetAccessor || hasAutoPropertyGet))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the field also be readonly if all manually implemented get and set accessors are declared readonly?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a slight leaning towards "no", but not a strong opinion.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not valid to declare both accessors as readonly, you'd have to declare the containing property as readonly instead, which would make the field readonly per these rules.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Declaring both as readonly is an error, but we may want to treat the backing field as readonly in the following case:

struct S
{
    object P { get; readonly set { } }
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a more compelling example is:

struct S
{
    object P { readonly get => field; init; }
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, I can't think of a case where things are made worse by the backing field not being readonly, in cases involving the accessors being marked readonly but not the containing type or property.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a PROTOTYPE comment and a test for now.

}
// - The property has no set accessor (but may have an init accessor) and
// the get accessor, if any, is automatically implemented.
else if ((!hasSetAccessor || isInitOnly) && (!hasGetAccessor || hasAutoPropertyGet))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a slight leaning towards "no", but not a strong opinion.

@RikkiGibson RikkiGibson self-assigned this Aug 28, 2024
<Q5>k__BackingField: True
<Q6>k__BackingField: {{useReadOnlyType}}
<Q7>k__BackingField: True
<Q8>k__BackingField: {{useReadOnlyType}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels like in the case useReadOnlyType: false, useReadOnlyMember: true, all accessors are readonly, yet the backing field is not readonly. That seems a little strange. I guess it doesn't really matter, though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is an example of the case I was expecting in #74922 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers New Feature - Field Keyword untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants