I hate class destructors with a burning passion

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Wed Jun 9 04:58:49 UTC 2021


On 6/8/21 10:37 PM, Walter Bright wrote:
> Throwing constructors is one of those things that makes a program very 
> hard to reason about. I've debated with Andrei about requiring that 
> constructors be nothrow. My advice is make them nothrow, i.e. design 
> them so they cannot fail. There's not a single throwing constructor in 
> DMD, and it's going to stay that way :-)

Except for FileMapping, and that's a Good Thing(tm):

https://github.com/dlang/dmd/pull/12560/files?file-filters%5B%5D=.c&file-filters%5B%5D=.d&file-filters%5B%5D=.md#diff-0be8539d9165e1607c7b47964ebf59a507c9ab436c6c4350df36d1c61d111a50R74

I'm half joking - that cosntructor may terminate the application.

Throwing constructors are an important part of achieving good 
encapsulation because they allow avoiding "invalid" states of objects 
altogether. In fairness, the fact that D has no user-definable default 
constructors weakens that argument (and is a weakness of the language 
itself that we'd do good to fix).


More information about the Digitalmars-d mailing list