funky property error with assoc array
Jonathan M Davis
jmdavisProg at gmx.com
Thu Nov 15 08:43:40 PST 2012
On Thursday, November 15, 2012 14:21:41 Dan wrote:
> 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.
Doing it from a postblit is impossible. Doing it from a copy constructor _is_
possible (assuming that they're added to the language - they don't help you
any until they are). The core problem with postblit constructor is that it
does a shallow copy _before_ entering the postblit constructor. This seems
great at first, because then you only have to copy stuff in the postblit itself
which actually needs a deep copy. But it fails completely with const and
immutable, because it's illegal to alter anything which is const or immutable
- even by casting them to mutable temporarily to make the change (doing so is
undefined behavior). So, you can't change _anything_ in a const postblit
constructor, meaning that you can't make a deep copy of anything.
A copy constructor, on the other hand, does not do any copying beforehand. It
all must still be constructed. The copy constructor then constructs the new
object with deep copies of the data. This works with const and immutable,
because it's construction rather than mutation. The postblit constructor fails
because it requires you to use mutation, which doesn't work with const.
So, as nice an idea as it is, the postblit constructor is a failure. And as
such, I believe that Walter and Andrei intend to replace it with copy
constructors in the long term (though the postblit constructor will probably
stay in the language for compatibility for a very, very long time, if not
permanently). Unfortunately however, nothing has been done about this on the
implementation side of things (they've only discussed it, not done anything
about it), so right now, we don't have a solution, since it's not like you can
go and declare your own copy constructor and have it work, because it'll never
be called unless you call it explicitly. Rather, the language would use the
postblit constructor if it's there and complain about it not working with
const if you try it, even if you have a copy constructor which would do the
job.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list