Would love to override default ctor of struct

Nick Treleaven nick at geany.org
Thu Jan 31 11:42:35 UTC 2019


On Tuesday, 22 January 2019 at 02:18:37 UTC, Jonathan M Davis 
wrote:
> It also would be bad for porting code to D from languages like 
> C++, because without extra work from the programmer, the code 
> would assume that this() was a default constructor when it 
> wasn't, making it easy to end up with subtle bugs. By outright 
> making this() illegal, D forces the programmer to deal with the 
> situation rather than allowing silent bugs.

D could solve this by only allowing a nullary constructor when 
there's `@disable this();`. The fact that `@disable this()` 
exists undermines the reason for a blanket ban on `this(){...}`, 
and I expect it's this way just because disable this was added 
later. This is worth having and would support Mutex, HourGlass, 
etc.

Besides runtime immutable initialization not working in a free 
function 'constructor' as you mentioned, not allowing a nullary 
constructor breaks the variadic constructor pattern in generic 
code when no arguments are passed.



More information about the Digitalmars-d mailing list