Start with:

struct X {
  char c[];
}

Assume you want value semantics - postblit provides this capability. It appears with 2.061 'this(this) const' is now supported. Previously, only 'this(this)' was recognized (i.e. called) when expected. To get value semantics a developer must choose between these two and for this case the goal is to ensure 'c = c.dup;' so value semantics are preserved.

Case 1: Go with 'this(this) { c = c.dup; }'. This works just fine except for the case where you want a const(X) as a member of some other class. For example: struct Y { const(X) x; }.

Case 2: Go with 'this(this) const { c = c.dup; }'. This is not possible because you can not change 'c' since the function is const. Maxim Fomin pointed out a workaround.

struct X {
  char c[];
  void _postblit_() { c = c.dup; }
  this(this) const {
    void delegate() dg = &_postblit_;
    dg();
  }
}

This workaround succeeds, but, based on this thread (http://forum.dlang.org/thread/[email protected]) I don't know if it is a bug or not. If it is not a bug, I would doubt it is the prescribed approach for doing this?

So, the question is:
What is the best *current* way to code X to achieve struct Y { const(X) x; } and what is the long run solution to this(this)?

Thanks,
Dan

Reply via email to