Would love to override default ctor of struct
Alex
sascha.orlov at gmail.com
Mon Jan 21 22:56:54 UTC 2019
On Monday, 21 January 2019 at 22:48:41 UTC, Jonathan M Davis
wrote:
>
> A classic case would be some uses of RAII such as what MFC does
> with its hourglass. You get code something like
>
> Hourglass hg;
>
> and that does everything. The constructor pops up the
> hourglass, and the destructor takes it down. If you wanted an
> object like that in D, you would have to use a factory
> function. e.g.
>
> auto hg = Hourglass.create();
>
> and you would then have to make it so that the object can deal
> with the fact that the init value never when through the
> factory function and thus must do nothing in its destructor
> (using @disable this() to disable default initialization
> reduces that problem, but since the init value can still be
> used explicitly, it really doesn't eliminate the problem).
>
> Similarly, as Meta alludes to in his example, having dynamic
> arrays of mutable elements as member variables is problematic
> with structs, because all of the instances of that struct on
> the same thread have a dynamic array that is a slice of the
> same chunk of memory, meaning that mutating an element in one
> mutates all of them (at least all of them which haven't ended
> up with their array being reallocated due to appending or
> whatnot).
Ah!!
> In most cases, you'd really want a separate dynamic array for
> each instance of the struct, and D doesn't give a good way to
> do that with init values, forcing you to use a factory function
> instead of a constructor if you want to try and force it. And
> now that we can actually directly initialize member variables
> which are pointers or class references, those join the ranks of
> potentially having problems due to all of the instances of that
> struct on a particular thread having the same value for those
> pointers or references. So, it can become a bit error-prone for
> those types of member variables.
>
> And really, any and all situations where you're looking to have
> a piece of code run when an instance of the struct is created
> but where you don't necessarily have arguments for the
> constructor is going to be harder to deal with cleanly in D
> thanks to the lack of default constructors.
>
> In general, you just learn to live with it and use factory
> functions to deal with the problem in those cases where it pops
> up. So, it usually isn't a big deal, and it's mostly just
> something that folks new to D complain about, but having init
> values such that we cannot have default constructors for
> structs is an area in D that's a tradeoff, not a complete win.
> Many of us think that the tradeoffs involved are well worth it,
> but that doesn't change the fact that there are times when
> having a default constructor would be really nice, and the fact
> that you can't is then annoying.
Ok... see it now...
More information about the Digitalmars-d
mailing list