-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Proposal: primary constructors #9517
Comments
I believe primary constructors were proposed before and currently spec draft for records does have an optional class body so you can define other members in it. |
@alrz I cannot find the record spec in https://github.com/dotnet/roslyn/tree/future/docs/features |
@orthoxerox It's proposed in #206 in the same form that you've presented here. |
@alrz looks like it has been excluded from the working draft in the sources. I would've said "great minds think alike", but this proverb is much less flattering in Russian. |
Instead of deriving the name for the auto property, why not just use the name as it is? Instead of
You would have
|
@Miista that will simplify a lot of internal plumbing for records, but every single FxCop in existence will throw a tantrum when it sees the resulting assembly. |
And that is a problem because? |
Because written code is a social interaction between code writers and FxCop and StyleCop validate the rules of engagement, @Miista. |
I just don’t see why FxCop or StyleCop should dictate how the language should evolve. Shouldn't FxCop and StyleCop adapt to how C# evolves, not the other way around? |
I think something like this were proposed, sealed class Person(string firstName : FirstName, string lastName : LastName); But I didn't see anything like that recently, perhaps it's discarded and the following is the accepted convention, sealed class Person(string FirstName, string LastName); To be honest, it's overkill to write a record like that and repeat every member's name twice. |
Closed in favor of #10154 |
To introduce positional deconstruction and bridge the gap between records and full-scale classes, I propose the addition of primary constructors.
Primary constructors for structs and standalone classes
A class with a primary constructor looks like this:
What does a primary constructor do?
uiModel
->UIModel
)Equals(object)
,GetHashCode()
,IEquatable<T>.Equals(T)
andToString()
, unless they are defined in the class/struct bodyWhat does a primary constructor require from the user's code?
Primary constructors for class hierarchies
A class with a primary constructor can inherit from a class with a primary constructor:
A class wthout a primary constructor can inherit from a class with a primary constructor:
A class with a primary constructor can inherit from a class without one only if the base class has a parameterless constructor.
An example of primary constructor expansion done by the compiler
The text was updated successfully, but these errors were encountered: