Bypassing the postblit?

Maxim Fomin maxim at maxim-fomin.ru
Mon Dec 30 08:18:21 PST 2013


On Sunday, 29 December 2013 at 19:26:09 UTC, monarch_dodra wrote:
> Nope. What you are asking for is basically a default 
> constructor, which D does not provide. Workarounds include the 
> "static opCall" pattern, as well as the "function builder" 
> pattern (eg: "MyStruct myStruct(Args args)")

Actually it does provide, but does not allow to override it. When 
you define postblit, dmd generates default constructor like this:

struct S
{
    int data[];
    this(this) { data = data.dup; }
    void __cpctor(const ref S s) const
    {
       this = s;
       s.__postblit();
    }
}

The only way to override it is currently is to make your 
function, drop original symbol from binary file and link the 
program.
I think it could work like opAssign - provide default version but 
allow to place yours. In such case it would be possible to alter 
original struct object but it would lead to conflict if original 
object is const or immutable qualified.


More information about the Digitalmars-d mailing list