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

Open questions for field-backed properties #8231

Merged
merged 5 commits into from
Jun 21, 2024

Conversation

cston
Copy link
Member

@cston cston commented Jun 20, 2024

No description provided.

@cston cston requested a review from a team as a code owner June 20, 2024 18:20
@cston cston requested review from CyrusNajmabadi and jnm2 June 20, 2024 18:20
@KennethHoff
Copy link

Aren't all auto properties "field backed"? That feels like a bad name for what is essentially a subset.

I prefer semi-auto while realizing that there's a bias there; it's always been referred to as semi-auto, so I'm naturally more inclined to call it that.

}
```

In the example above, binding to the backing field should result in an error: "initializer cannot reference non-static field".
Copy link
Member

Choose a reason for hiding this comment

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

Is there an example where field binding to the backing property would have desirable behavior?

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 haven't found an example where binding to the backing field could be used in the property initializer.

Copy link
Member

Choose a reason for hiding this comment

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

If so then why do we recommend making it a keyword? Seems like a breaking change that isn't balanced with a positive scenario.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, if we did make field a keyword in property initializer, I would expect to actually be able to read the field in the initializer if the property is static. I feel like it gets wonky.

Copy link
Contributor

Choose a reason for hiding this comment

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

Got a weirder one, which isn't on the list (and I don't know that I've seen it mentioned elsewhere). What about properties with expression body definitions?

public int X => Increment(ref field);

private int Increment(ref int field) => field++;

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah those are property accessors and we expect field to be a keyword in them.

Copy link
Member Author

@cston cston Jun 21, 2024

Choose a reason for hiding this comment

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

It seems like the backing field of a static property might be usable in the property initializer.

class MyClass
{
    static object Property { get; } = Initialize(out field);
    
    static object Initialize(out object obj)
    {
        return obj = new Random().Next();
    }
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah that's wonky and not useful IMO (you're already assigning the field, why do you need to be able to otherwise use it?). So put me in for wanting 'field' to just not be a keyword in the initializer, even though it might feel a little confusing when an accessor is using 'field' keyword in the same property.

Copy link
Contributor

Choose a reason for hiding this comment

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

Got a weirder one, which isn't on the list (and I don't know that I've seen it mentioned elsewhere). What about properties with expression body definitions?

@HaloFour This was part of the intended design from the beginning, supporting lazy initialization such as that example and => field ??=. #8069 fleshes out the proposal with examples like these.

@hez2010
Copy link

hez2010 commented Jun 21, 2024

Aren't all auto properties "field backed"?

You can definitely create a property which is not backed by a field:

public int CurrentNumber => Random.Shared.Next();

@KennethHoff
Copy link

@hez2010 that's not an auto property though, is it? I might be wrong, but I thought an auto property was properties where you don't reference any storage area (basically just { get; set/init; })

@cston
Copy link
Member Author

cston commented Jun 21, 2024

@dotnet-policy-service rerun

@cston cston merged commit c181bab into dotnet:main Jun 21, 2024
1 check passed
@cston cston deleted the field-open-questions branch June 21, 2024 23:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants