Thread overview
[Issue 23973] static constructors should have to be nothrow
Jun 06, 2023
Walter Bright
Jun 13, 2023
RazvanN
Jun 13, 2023
Dlang Bot
June 06, 2023
https://issues.dlang.org/show_bug.cgi?id=23973

Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla@digitalmars.com

--- Comment #1 from Walter Bright <bugzilla@digitalmars.com> ---
Sounds good to me.

--
June 13, 2023
https://issues.dlang.org/show_bug.cgi?id=23973

RazvanN <razvan.nitu1305@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |razvan.nitu1305@gmail.com

--- Comment #2 from RazvanN <razvan.nitu1305@gmail.com> ---
Would make sense to apply the same constraints for static destructors? Actually, I think that that would make sense for constructors and destructors in general.

--
June 13, 2023
https://issues.dlang.org/show_bug.cgi?id=23973

--- Comment #3 from Dlang Bot <dlang-bot@dlang.rocks> ---
@RazvanN7 created dlang/dmd pull request #15318 "Fic Issue 23973 - static constructors should have to be nothrow" mentioning this issue:

- Fic Issue 23973 - static constructors should have to be nothrow

https://github.com/dlang/dmd/pull/15318

--
July 13
https://issues.dlang.org/show_bug.cgi?id=23973

Jonathan M Davis <issues.dlang@jmdavisProg.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |issues.dlang@jmdavisProg.co
                   |                            |m

--- Comment #4 from Jonathan M Davis <issues.dlang@jmdavisProg.com> ---
(In reply to RazvanN from comment #2)
> Would make sense to apply the same constraints for static destructors? Actually, I think that that would make sense for constructors and destructors in general.

Constructors in general are a very different issue from static constructors. Throwing from constructors on types is an extremely normal thing to do. Without that, you're often forced to do two step initialization in order to be able to tell the caller that the arguments are invalid. Disallowing throwing from normal constructors would very much be an anti-pattern.

It _might_ make sense to require that destructors on types be nothrow, but I'm not sure. C++ went and made noexcept the default for destructors, and their standard library doesn't allow destructors to throw exceptions, but IIRC, some projects actually do it. So, as I understand it, the C++ community considers it something that should generally be avoided but not necessarily something that should absolutely never be done. So, I think that we'd have to look at the situation very carefully before disallowing it in D.

Regardless, the question of static constructors and destructors and nothrow is very different from non-static ones, and the issues that Atila brought up with static constructors do not apply to regular constructors at all outside of them being called from static constructors - and those issues then apply to any code that would throw an exception from within a static constructor.

--