digitalmars.D.learn - Do classes require explicit constructors?
- Brother Bill (39/39) Feb 14 In Programming in D book, page 324, I've expanded this code as
- Richard (Rikki) Andrew Cattermole (5/9) Feb 14 D's classes were based off of Java's.
- Brother Bill (9/19) Feb 14 Eiffel may be the only language that supports inheriting
- Richard (Rikki) Andrew Cattermole (9/31) Feb 14 Swift does it too.
- Mike Parker (4/12) Feb 14 How many exception constructors do you write? Minor annoyance,
- Richard (Rikki) Andrew Cattermole (13/32) Feb 14 Zero, because I absolutely hate doing it.
- Brother Bill (7/8) Feb 14 On that topic, consider supporting true multiple inheritance,
- Richard (Rikki) Andrew Cattermole (4/16) Feb 14 This has come up a lot over the years, especially in regards to C++.
- Mindy (0xEAB) (6/8) Feb 22 While it may be a copy&paste task, I’ve ran into mistakes with
- Mindy (0xEAB) (4/13) Feb 22 Additional noteworthy issues include missing ingredients of the
- Andy Valencia (9/11) Feb 15 I believe this all goes back to Smalltalk, where constructors are
- Steven Schveighoffer (22/27) Feb 14 If you don't define a constructor, a default no-argument
In Programming in D book, page 324, I've expanded this code as
follows:
source/app.d
```
void main()
{
auto variable1 = new MyClass(1);
auto variable2 = new MyClass(2);
// variable2 disassociates from new MyClass(2), where GC
(garbage collector) can reclaim its memory
// now both variable1 and variable2 are both referencing new
MyClass(2)
variable1 = variable2;
assert(variable1 is variable2);
}
class MyClass
{
int id;
// this(int id)
// {
// this.id = id;
// }
}
```
This fails to compile with message:
```
c:\temp\c54_p324_1d_assignment\source\app.d(5): Error: no
constructor for `MyClass`
auto variable1 = new MyClass(1);
^
c:\temp\c54_p324_1d_assignment\source\app.d(6): Error: no
constructor for `MyClass`
auto variable2 = new MyClass(2);
```
Is this expected behavior, that is, we always need to create our
constructors, no free constructor is built for us by the D
compiler?
gets a free constructor if no explicitly declared constructors.
Feb 14
On 15/02/2026 4:42 AM, Brother Bill wrote:Is this expected behavior, that is, we always need to create our constructors, no free constructor is built for us by the D compiler?Yes currently.free constructor if no explicitly declared constructors.D's classes were based off of Java's. I want some changes to them, constructors also don't inherit. They are massive pain for exceptions.
Feb 14
On Saturday, 14 February 2026 at 15:45:20 UTC, Richard (Rikki) Andrew Cattermole wrote:On 15/02/2026 4:42 AM, Brother Bill wrote:Eiffel may be the only language that supports inheriting constructers. In Eiffel, a constructor is just another method, that is marked as a constructor in "create" clause. As all methods are inheritable by default in Eiffel, constructors can be inherited there.Is this expected behavior, that is, we always need to create our constructors, no free constructor is built for us by the D compiler?Yes currently.gets a free constructor if no explicitly declared constructors.D's classes were based off of Java's. I want some changes to them, constructors also don't inherit.They are massive pain for exceptions.Why are they a massive pain? Please explain.
Feb 14
On 15/02/2026 4:50 AM, Brother Bill wrote:On Saturday, 14 February 2026 at 15:45:20 UTC, Richard (Rikki) Andrew Cattermole wrote:Swift does it too. There is a bunch of "fun" rules surrounding it though. I think we can simplify it right down though.On 15/02/2026 4:42 AM, Brother Bill wrote:Eiffel may be the only language that supports inheriting constructers. In Eiffel, a constructor is just another method, that is marked as a constructor in "create" clause. As all methods are inheritable by default in Eiffel, constructors can be inherited there.Is this expected behavior, that is, we always need to create our constructors, no free constructor is built for us by the D compiler?Yes currently.free constructor if no explicitly declared constructors.D's classes were based off of Java's. I want some changes to them, constructors also don't inherit.You have to manually write the constructors for every exception you want to declare yourself. Normally with IDE's you'd have assistance and it would be done automatically, but that isn't normal for D. There are multiple parameters. Not a joy.They are massive pain for exceptions.Why are they a massive pain? Please explain.
Feb 14
On Saturday, 14 February 2026 at 15:54:23 UTC, Richard (Rikki) Andrew Cattermole wrote:How many exception constructors do you write? Minor annoyance, maybe, but massive pain? Really?You have to manually write the constructors for every exception you want to declare yourself. Normally with IDE's you'd have assistance and it would be done automatically, but that isn't normal for D. There are multiple parameters. Not a joy.They are massive pain for exceptions.Why are they a massive pain? Please explain.
Feb 14
On 15/02/2026 4:57 AM, Mike Parker wrote:On Saturday, 14 February 2026 at 15:54:23 UTC, Richard (Rikki) Andrew Cattermole wrote:Zero, because I absolutely hate doing it. Same reason why I avoid class hierarchies like the plague. There are some real irritants with it that don't need to exist.How many exception constructors do you write?You have to manually write the constructors for every exception you want to declare yourself. Normally with IDE's you'd have assistance and it would be done automatically, but that isn't normal for D. There are multiple parameters. Not a joy.They are massive pain for exceptions.Why are they a massive pain? Please explain.Minor annoyance, maybe, but massive pain? Really?To me yes. From Java, the experience was a total 180. I went from yeah ok I need this exception, go declare it. To let's just throw an Exception forget about doing this properly. It breeds slop, which really grates on me. Usually I can get away with ignoring these features, but with PhobosV3 I am unable to. I've already had to add new exception classes to druntime specifically for it as part of null check. I want classes improved, there is plenty of room for it.
Feb 14
On Saturday, 14 February 2026 at 16:08:15 UTC, Richard (Rikki) Andrew Cattermole wrote:I want classes improved, there is plenty of room for it.On that topic, consider supporting true multiple inheritance, clean like Eiffel, but unlike C++, which did it dirty. Multiple inheritance in D will be a teeny bit slower at run time, and a bit slower at compile time. If you did it, you would have Eiffel with D syntax.
Feb 14
On 15/02/2026 5:21 AM, Brother Bill wrote:On Saturday, 14 February 2026 at 16:08:15 UTC, Richard (Rikki) Andrew Cattermole wrote:This has come up a lot over the years, especially in regards to C++. It is intentional to not support it, it makes the implementation a lot more complex.I want classes improved, there is plenty of room for it.On that topic, consider supporting true multiple inheritance, clean like Eiffel, but unlike C++, which did it dirty. Multiple inheritance in D will be a teeny bit slower at run time, and a bit slower at compile time. If you did it, you would have Eiffel with D syntax.
Feb 14
On Saturday, 14 February 2026 at 15:57:09 UTC, Mike Parker wrote:How many exception constructors do you write? Minor annoyance, maybe, but massive pain? Really?While it may be a copy&paste task, I’ve ran into mistakes with accidentally pasting unsuitable constructors with additional parameters, making typos while fixing things up, missing `super()` calls etc. Seems quite a bit of a hassle for something that could be a trivial task.
Feb 22
On Sunday, 22 February 2026 at 09:10:10 UTC, Mindy (0xEAB) wrote:On Saturday, 14 February 2026 at 15:57:09 UTC, Mike Parker wrote:Additional noteworthy issues include missing ingredients of the attribute soup or accidental omission of `super()` call parameters.How many exception constructors do you write? Minor annoyance, maybe, but massive pain? Really?While it may be a copy&paste task, I’ve ran into mistakes with accidentally pasting unsuitable constructors with additional parameters, making typos while fixing things up, missing `super()` calls etc. Seems quite a bit of a hassle for something that could be a trivial task.
Feb 22
On Saturday, 14 February 2026 at 15:50:46 UTC, Brother Bill wrote:Eiffel may be the only language that supports inheriting constructers.I believe this all goes back to Smalltalk, where constructors are just methods on the single object representing a given class. That class object inherits from the superclass, and inherited constructors are thus not a special case at all. Busy-work plumbing of constructors in D is esthetically displeasing in a mostly elegantly designed language. But I would not call it a big deal. Shrug. Andy
Feb 15
On Saturday, 14 February 2026 at 15:42:15 UTC, Brother Bill wrote:Is this expected behavior, that is, we always need to create our constructors, no free constructor is built for us by the D compiler?If you don't define a constructor, a default no-argument constructor is provided for you. If you want a constructor with parameters, you have to define it. ```d class C { int i; } struct S { int i; } void main() { auto c = new C(); //ok auto s = new S(); //ok auto s2 = new S(1); //ok, default ctor takes optional parameters for fields auto c2 = new C(1); //error } ```gets a free constructor if no explicitly declared constructors.args. -Steve
Feb 14









"Richard (Rikki) Andrew Cattermole" <richard cattermole.co.nz> 