Spec change around backing field being readonly#5575
Spec change around backing field being readonly#5575CyrusNajmabadi merged 2 commits intodotnet:mainfrom
Conversation
proposals/semi-auto-properties.md
Outdated
|
|
||
| ```diff | ||
| - If the auto-property has no set accessor, the backing field is considered `readonly` ([Readonly fields](classes.md#readonly-fields)). Just like a `readonly` field, a getter-only auto-property can also be assigned to in the body of a constructor of the enclosing class. Such an assignment assigns directly to the readonly backing field of the property. | ||
| + If the auto-property has semicolon-only get accessor without a set accessor, the backing field is considered `readonly` ([Readonly fields](classes.md#readonly-fields)). Just like a `readonly` field, a semicolon-getter-only auto-property can also be assigned to in the body of a constructor of the enclosing class. Such an assignment assigns directly to the readonly backing field of the property. |
There was a problem hiding this comment.
I'm not sure if "semicolon-getter-only" is clear enough.
There was a problem hiding this comment.
It is to me, or it could be an auto property which only has a semicolon getter. I'd nitpick:
- has a semicolon-only get accessor
auto propertynotauto-property(I know, this was original). Hyphenation is for when the two words are acting in combination as a modifier (adjective) of some other noun, but here, 'property' is the noun.
There was a problem hiding this comment.
I wonder if the spec should say something about getter-only properties being assignable in the body of a constructor even when the getter is not semicolon-only and therefore the backing field is not readonly. Example:
class C
{
public C(string? p) { P = p; }
public string P => field ??= CalculateP();
private string CalculateP() => throw new NotImplementedException();
}There was a problem hiding this comment.
Yup I just noticed an overlap and a possibility to divide this into two separate sentences depending on what the initializer behavior would be. Let me mark as a draft for now as it doesn't seem to be accurate.
There was a problem hiding this comment.
I think the wording is more accurate now?
|
thanks! :) |
|
It feels like if you had a
Does the spec account for this as written? |
|
The spec doesn't account for that currently. Let me open a new PR for that. |
…dates * upstream/main: Add example of when field initializers will not be run for option 3. Correct link Added LDM notes for January 3rd, 2022. Update README.md Update LDM agenda Fix line wrapping (dotnet#5589) Update raw-string-literal.md 'record struct' constructor requires 'this' initializer that calls primary constructor or explicit constructor (dotnet#5562) Tweak readonly backing field wording (dotnet#5583) Update semi auto props spec around readonly backing field for init-only (dotnet#5582) Spec change around backing field being readonly (dotnet#5575)
* upstream/main: Update LDM agenda Added LDM Notes for January 5th, 2022. Update the required members spec (dotnet#5566) Add example of when field initializers will not be run for option 3. Correct link Added LDM notes for January 3rd, 2022. Update README.md Update LDM agenda Fix line wrapping (dotnet#5589) Update raw-string-literal.md 'record struct' constructor requires 'this' initializer that calls primary constructor or explicit constructor (dotnet#5562) Tweak readonly backing field wording (dotnet#5583) Update semi auto props spec around readonly backing field for init-only (dotnet#5582) Spec change around backing field being readonly (dotnet#5575)
cc @AlekseyTs @CyrusNajmabadi