Skip to content

Readonly structs in standard library #893

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

Closed
KalleOlaviNiemitalo opened this issue Aug 16, 2023 · 4 comments · Fixed by #904
Closed

Readonly structs in standard library #893

KalleOlaviNiemitalo opened this issue Aug 16, 2023 · 4 comments · Fixed by #904
Assignees
Milestone

Comments

@KalleOlaviNiemitalo
Copy link
Contributor

Describe the bug

Of the struct types listed in Annex C of the C# 7.x draft, only a few are specified as readonly struct. In .NET however, most of them are defined as readonly struct.
Is this allowed, and is it an extension?

Example

Types that are readonly struct in .NET but plain struct in Annex C include:

  • System.Int32
  • System.Span<T>
  • System.Runtime.CompilerServices.TaskAwaiter<TResult>

Expected behavior

Could be solved in any of these ways:

  • make them readonly struct in the standard, too
  • declare that an implementation can add readonly to standard library struct types
  • as above, but also change every readonly struct in Annex C to plain struct

Additional context

Similar to #870 on attributes.

@KalleOlaviNiemitalo
Copy link
Contributor Author

See also #894 for an effect of readonly struct outside the struct type itself.

@jskeet jskeet added this to the C# 7.x milestone Aug 16, 2023
@jskeet
Copy link
Contributor

jskeet commented Aug 16, 2023

Hmm... I'm tempted to go with the first option, although we'll probably need an explanatory note to explain why they're not the same as in ECMA 335.

@BillWagner, preferences for this? (I'm happy to create the PR.)

@BillWagner
Copy link
Member

I like the first option with the Note: as well.

@jskeet jskeet self-assigned this Aug 16, 2023
@jskeet
Copy link
Contributor

jskeet commented Aug 16, 2023

Right - I'll try to prep a PR soon.

jskeet added a commit to jskeet/csharpstandard that referenced this issue Aug 17, 2023
The remaining non-readonly structs are:

- `Nullable<T>` (not sure why)
- `ValueTuple<...>` (as it's mutable)

Fixes dotnet#893.
jskeet added a commit to jskeet/csharpstandard that referenced this issue Aug 18, 2023
The remaining non-readonly structs are:

- `Nullable<T>` (not sure why)
- `ValueTuple<...>` (as it's mutable)

Fixes dotnet#893.
jskeet added a commit that referenced this issue Aug 18, 2023
The remaining non-readonly structs are:

- `Nullable<T>` (not sure why)
- `ValueTuple<...>` (as it's mutable)

Fixes #893.
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 a pull request may close this issue.

3 participants