Taking a copy of an object
kris
foo at bar.com
Thu Aug 3 23:47:47 PDT 2006
Kirk McDonald wrote:
[snip]
> fn( i, j, :obj); // send a copy of obj to the function
>
> So, for both the proposed := copy-assignment operator and unary : copy
> operator, the various copying mechanisms would be:
>
> class instances: copy constructor
> arrays/AAs: dup property
> structs, primitive types, etc: by-value copy
>
> We wouldn't even have to provide a copy constructor in Object, you know.
> Trying to use these operators on an instance of a class that doesn't
> define a copy constructor would just be a compiler error, like any other
> undefined operator overload or method you try to use.
>
> Thoughts?
>
yeah :)
Sorry, Kirk, but this kinda stinks:
# fn( i, j, :obj);
I don't see any point of introducing obtuse operators when consistency
would dictate something like this:
# fn( i, j, obj.dup);
The compiler can just as easily support a generic (shallow) .dup for
classes as it can for other types. Alternatively, simply add a final
dup() method to the root Object?
Does deep-copy really need an operator? I have to wonder whether a
simple naming-convention would do the trick instead ... that way, you'd
never have a /default/ deep-copy, and usage mistakes would produce a
most welcome compile-time error.
More information about the Digitalmars-d
mailing list