Allow ref struct
primary constructor to capture ref-like (inside) parameters
#8956
Replies: 3 comments 16 replies
-
If all PC params are auto-promoted to fields, it becomes a lot more like a record, no? Not saying it's a bad thing, but definitely a slightly different behavior than PCs on classes and regular structs. |
Beta Was this translation helpful? Give feedback.
-
An automatic promotion or only when they are used? |
Beta Was this translation helpful? Give feedback.
-
Think the idea of allowing a There are complications to consider though when this is extended to all ref like arguments. Consider for example the following: ref struct S1(Span<byte> span)
{
// legal
Span<byte> Span => span;
}
ref struct S2(ref Span<byte> span)
{
// can't be legal
Span<byte> Span => span;
} The reason being that you cannot have Beyond that I'm still a bit hesitant about the idea of allowing capture of |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
A
ref struct
can have a primary constructor, just as regular classes/structs, however, C# will complain if you actually use the parameters anywhere other than initializing a field in the ref struct (see CS9110: Cannot use primary constructor parameter that has ref-like type inside an instance member.)This proposal asks for this restriction to be lifted, allowing primary constructors to capture any ref-like parameter that is used by the type.
Beta Was this translation helpful? Give feedback.
All reactions