Skip to content
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

Chaining-only constructors #14018

Closed
jskeet opened this issue Sep 23, 2016 · 5 comments
Closed

Chaining-only constructors #14018

jskeet opened this issue Sep 23, 2016 · 5 comments

Comments

@jskeet
Copy link

jskeet commented Sep 23, 2016

I've only just found out about expression-bodied everything potentially making it into a later edition of C#, whilst writing about what you can't do with expression-bodied members in the 4th ed of C# in Depth :)

Given the status of that feature, I doubt that this feature request will make it into the same release, but has there been any consideration of constructors with no body, rather than those with a body which is a single statement?

In my experience, I have two kinds of constructor:

  • Ones which chain to the base class constructor, and then set state in the current object base on parameters
  • Ones which chain to another constructor in the current class, and then do nothing else.

In other words, where properties/methods/etc having 1 statement is a sort of natural base case, for constructors it feels like having 0 statements is a natural base case. It would be nice to allow this with just a semi-colon.

For example, from Noda Time's LocalDate class,

public LocalDate([NotNull] Era era, int yearOfEra, int month, int day)
    : this(era, yearOfEra, month, day, CalendarSystem.Iso)
{
}

would become

public LocalDate([NotNull] Era era, int yearOfEra, int month, int day)
    : this(era, yearOfEra, month, day, CalendarSystem.Iso);

Apologies if this has already been covered in a discussion somewhere that I've failed to find. Suggest @gafter comments first, being close to the new feature. (And hey, maybe it's already implemented there and I didn't spot it in the tests...)

@i3arnon
Copy link

i3arnon commented Sep 23, 2016

What about regular expression-bodied methods with no body (e.g. an empty virtual method)?
If empty constructors can end with a semicolon you would expect a regular method will as well.
That can be easily confused with abstract method syntax though.

@svick
Copy link
Contributor

svick commented Sep 23, 2016

Would it be enough to just make this an acceptable style?

public LocalDate([NotNull] Era era, int yearOfEra, int month, int day)
    : this(era, yearOfEra, month, day, CalendarSystem.Iso) {}

@gafter
Copy link
Member

gafter commented Sep 23, 2016

The proposal is to reduce the syntax to allow one character instead of two for this one infrequent case. That doesn't seem worth the bother to me.

@jskeet
Copy link
Author

jskeet commented Sep 23, 2016

The benefit wouldn't be so much the single-character reduction (although I note that expression-bodied constructors don't reduce the character count at all...) but the clearer indication of "this constructor just chains to another, it's not intended to do anything else".

But I'm certainly not so attached to the idea that I'm going to argue for it vehemently :)

@jskeet jskeet closed this as completed Sep 23, 2016
@jskeet jskeet reopened this Sep 23, 2016
@jskeet jskeet closed this as completed Sep 23, 2016
@alrz
Copy link
Member

alrz commented Sep 23, 2016

the clearer indication of "this constructor just chains to another, it's not intended to do anything else

I've actually thought this is about an access modifier for constructors that only permits usage in the constructor initializer; basically the same statement inverted: the indication of "this constructor is intended to be chained to another and should not be used as a standalone constructor"; On the second thought, it could be implemented via an analyzer.

PS: Same idea for void virtual: #8985.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants