Would love to override default ctor of struct

aliak something at something.com
Tue Jan 22 01:23:21 UTC 2019


On Saturday, 19 January 2019 at 11:13:22 UTC, Jonathan M Davis 
wrote:
> On Saturday, January 19, 2019 3:05:22 AM MST Dru via 
> Digitalmars-d wrote:
>> What is the idea behind why we can't define default ctor for 
>> structs?
>>
>> In current situation I need to "@disable this()"
>> then define a constructor with dummy arguments  (because my
>> constructor does not need arguments)
>>
>> It is a big pain on syntax
>
> Structs in D don't actually have default constructors. Rather, 
> they have a default value that they're initialized to - namely 
> T.init (where T is the type name). In fact, all types in D have 
> a default value so that they're guaranteed to not be garbage if 
> you forget to initialize them with a specific value. This gets 
> taken advantage of in a number of places. One obvious one is 
> arrays. The init value can simply be blitted into all of the 
> elements of the array. A number of features in D are built 
> around that and really don't work without it.


I've always wondered why T.init can't be it's own thing and 
separate from this(). So if someone defined a this() then D just 
treats it like a custom non-default constructor? Not possible?


More information about the Digitalmars-d mailing list