Skip to content

Define rules for "readonly" in struct types #1339

@BillWagner

Description

@BillWagner

See https://github.com/dotnet/csharpstandard/pull/673/files#r1510548259

The language that describes what readonly allows and prohibits needs more work.

  1. There are instances of the phrase "It is a compile-time error to attempt to modify the state of an instance struct variable via a readonly property declared in that struct." in the standard. This isn't defined as well as it should be.
  2. A compiler may allow readonly members to invoke non-readonly members on "the same instance" of a struct by creating a defensive copy and invoking the mutating member on the defensive copy.
  3. A readonly member can't change the value of a member, but can change the fields that are members of that member. (not specese, but that describes the issue)

Copied from the linked discussion:

under classes modifiers are generally described in subclauses under §15.3 Class members.

That same pattern can be repeated here with the current content of §16.3 Struct members becoming §16.3.1 General and adding §16.3.2 The readonly modifier.

In §16.3.2 the rules for readonly members can be given (not proposed text):

This would provide a single place for defining what a readonly member. The additional per-member differences could follow in §16.3 subclauses or remain as they are in §16.4 subclauses.

In addition to the normative language, an example (as discussed here would be useful as well.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions