On Wednesday, 14 November 2012 at 22:07:10 UTC, Jonathan M Davis wrote:

Postblit doesn't work with const objects and there's not currently any way to make it work with const objects. So, any attempt to copy any element in your AA won't work. So, if the AA implementation has any code in it which requires copying an element (and it probably does), then a type with a postblit in it
won't work if it's const.


Jonathan, thanks for taking the time on this and all my other questions. Coming from C++ and trying to hammer out how to use D I've hit this postblit many times and many ways. I agree that postblit does not work with const - and my feeling is maybe it should not and I hope there is a work around (for structs). For instance:
------------
import opmix.mix;
import std.stdio;
struct Big {
  char [] c ;
}
void main () {
  const ( Big ) s = { " test " };
  Big other = s.gdup;
  writeln(other);
}
------------

At present const and postblit don't get allow at all, and it sounds like the solution that Andrei and Walter have been cooking up will probably involve

I think that is fine they don't get along. postblit can not guarantee no data is shared after the call, so they should not. In the example here I don't access the map at all and the call works fine in non-property form.

adding copy constructors, but nothing has actually happened yet.

I do not see how copy constructors can help. Receiving const(T) or const(T) ref to anything and hoping to copy it is the challenge and doing it from a copy constructor is no easier than a postblit. I think bearophile pointed it out best:

   In practice to copy something const to something that's
   not const you need a deep copy function, because inside
   the array there can be other arrays that are const,
   etc. Transitive const requires transitive copy, it's
   easy :-)

If you have any extra cycles I would love feedback on (dup and Global Dup) in:
https://github.com/patefacio/d-help/blob/master/doc/canonical.pdf

Actually feedback on all would be great because it establishes how I want to work with structs and being new to D I'd like assurances it is safe/correct.

Thanks
Dan

postblit
fundamentally doesn't work with const or immutable though and can't, because you can't modify const or immutable objects, and postblit requires you to alter the object. So, as far as const and immutable go, postblit is
fundamentally flawed.

- Jonathan M Davis

Reply via email to