www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Do classes require explicit constructors?

reply Brother Bill <brotherbill mail.com> writes:
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
next sibling parent reply "Richard (Rikki) Andrew Cattermole" <richard cattermole.co.nz> writes:
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
parent reply Brother Bill <brotherbill mail.com> writes:
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:
 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.
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.
 They are massive pain for exceptions.
Why are they a massive pain? Please explain.
Feb 14
next sibling parent reply "Richard (Rikki) Andrew Cattermole" <richard cattermole.co.nz> writes:
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:
 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.
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.
Swift does it too. There is a bunch of "fun" rules surrounding it though. I think we can simplify it right down though.
 They are massive pain for exceptions.
Why are they a massive pain?  Please explain.
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.
Feb 14
parent reply Mike Parker <aldacron gmail.com> writes:
On Saturday, 14 February 2026 at 15:54:23 UTC, Richard (Rikki) 
Andrew Cattermole wrote:

 They are massive pain for exceptions.
Why are they a massive pain?  Please explain.
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.
How many exception constructors do you write? Minor annoyance, maybe, but massive pain? Really?
Feb 14
next sibling parent reply "Richard (Rikki) Andrew Cattermole" <richard cattermole.co.nz> writes:
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:
 
 They are massive pain for exceptions.
Why are they a massive pain?  Please explain.
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.
How many exception constructors do you write?
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.
 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
parent reply Brother Bill <brotherbill mail.com> writes:
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
parent "Richard (Rikki) Andrew Cattermole" <richard cattermole.co.nz> writes:
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:
 
 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.
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.
Feb 14
prev sibling parent reply Mindy (0xEAB) <desisma heidel.beer> writes:
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
parent Mindy (0xEAB) <desisma heidel.beer> writes:
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:
 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.
Additional noteworthy issues include missing ingredients of the attribute soup or accidental omission of `super()` call parameters.
Feb 22
prev sibling parent Andy Valencia <dont spam.me> writes:
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
prev sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
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