Why do Primary Constructors have a limitation that they must be a record class/struct to have a body that is just a semicolon? #6871
Replies: 3 comments 3 replies
-
See #6569. Primary constructors are still in design and hopefully will make it into the language soon(ish). As @Richiban mentions below, under the current proposal for how primary constructors will work, Due to C# being open source these days though, it's possible to play around with the current prototype, eg via the Primary Constructors branch in SharpLab. This link highlights the difference in terms of code that you'll get with the following too structs: struct Foo(int x, int y);
struct Bar(int x, int y)
{
public int X {get;} = x;
public int Y {get;} = y;
} |
Beta Was this translation helpful? Give feedback.
-
As far as I'm aware, this would be a useless type because in non-record types the class parameters function as captures (that can be lifted into fields if required by the class members). That means that your type struct Foo
{
public Foo(int x, int y)
{
}
} I can see an argument that it should be a warning rather than error, but it's still useless. |
Beta Was this translation helpful? Give feedback.
-
Thanks for bringing this up. We'll be discussing the restriction in LDM soon (likely next week, but no guarantees). |
Beta Was this translation helpful? Give feedback.
-
I was reading the Primary Constructor proposal, and this line jumped out at me:
Why this restriction? Some
seems useful.
Beta Was this translation helpful? Give feedback.
All reactions