Automatic Constructors #3019
Replies: 11 comments
-
I actually like the idea. |
Beta Was this translation helpful? Give feedback.
-
@uygary - How would it be a breaking change? It would not generate new constructors that already exist because if any already exist, it will not generate any at all. |
Beta Was this translation helpful? Give feedback.
-
I'd rather an explicit syntax to specify that the constructors are delegated, like C++. class A
{
public:
explicit A(int x) {}
};
class B: public A
{
using A::A;
}; |
Beta Was this translation helpful? Give feedback.
-
@TonyValenti here's how it would be a breaking change. Imagine you have this code:
At the moment the class With your proposed change, |
Beta Was this translation helpful? Give feedback.
-
@theunrepentantgeek under reflection everything is a breaking change. As far as language features go, this one ranks pretty low on the breaking-change-o-meter. |
Beta Was this translation helpful? Give feedback.
-
I agree @YairHalberstadt - but was following up on @uygary's comment earlier to demonstrate the issue. |
Beta Was this translation helpful? Give feedback.
-
I generally agree that it would rank low as well. I'm just pointing out that it still is a breaking change, that's all. |
Beta Was this translation helpful? Give feedback.
-
I have small addition for that idea - automatic constructors for |
Beta Was this translation helpful? Give feedback.
-
As a variant of proposal, we can introduce new syntax for inheriting:
Point is C# team should never apply "one of two" variants - it's stupid, it's unprofessional. If there is CHOICE of two GOOD PROPOSALS, you must offer BOTH variants. Like I offered above. Despite your experience, you can never know what business task we have. So we need choice. |
Beta Was this translation helpful? Give feedback.
-
The 👎 is for the unnecessary commentary. If your goal is to convince the team that they should be modifying the language to support this functionality calling anyone "stupid" or "unprofessional" is probably the fastest way to achieve the opposite. The C# team has made it very clear that they don't add language features for the sake of offering choice. Every new feature adds complexity to the language and the compiler which then must be supported indefinitely. If you disagree with that to the point where you feel the need to denigrate the C# team, maybe this isn't the language for you. |
Beta Was this translation helpful? Give feedback.
-
@WrongBit I believe that's not such a good idea as it wouldn't align well with the existing syntax. It would be specific to ctors, whereas As a side note, please refrain making remarks such as "it's stupid" unless you have links to some papers, that were published in peer-reviewed journals, which back that notion up. If a language design team goes down the "Let's throw everything into the language!" path, they end up with a language like D. While I kind of like many features and innovations it has, I'm not so sure that they made the best choice by turning it into the "language that has it all, and in darkness binds them." It's far too large to learn and use, far too feature-rich to maintain, and far less consistent than it could've turned out to be. I personally see and understand the need for this particular feature, and I like the idea. I hate boilerplate. |
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.
-
TLDR:
When no constructors are defined, the compiler should automatically generate constructors whose signatures match that of the immediate base class. Today, this happens with only the default constructor but should be expanded to all constructors. To provide compatibility with existing code, in the event that a developer defines any constructors at all, the compiler will not generate any automatic constructors for that class.
Details:
Here is an example of the compiler generating an automatic constructor today:
This saves a lot of boiler plate code.
I would like to see this expanded to handle another scenario. For example, I often bump into scenarios like this:
You'll notice that in the examples above, the only reason I'm recreating those constructors is so that creating a derived class takes the same parameters as the base class. It is basically a bunch of filler.
I would like to be able to write the above code as follows:
To provide compatibility with existing code, in the event that a developer defines any constructors at all, the compiler will not generate any automatic constructors for that class. For example:
Beta Was this translation helpful? Give feedback.
All reactions