Old problem with performance

Rainer Deyke rainerd at eldwood.com
Thu Feb 12 18:55:18 PST 2009


Tom S wrote:
> Rainer Deyke wrote:
>> Because you can't correctly duplicate a multi-dimensional dynamic array
>> in D by using the built-in 'dup' property.
> 
> Simply because shallow copies are the default. Usually you can't even
> have automatic deep copies. What if the array was supposed to be
> malloc'd or put in a contiguous memory block taken from a memory cache?
> A .dup that made a deep copy of it would introduce even a larger problem
>  (since it would seem correct) than remembering that dups/struct copies
> are shallow by default and you need special code to do otherwise.

C++ has a way of specifying how deep you want your copies to be at the
declaration point:

std::vector<boost::shared_ptr<std::vector<int> > > v1, v2;
v2 = v1; // Shallow copy.
std::vector<std::vector<int> > v3, v4;
v4 = v3; // Deep copy.

Granted, C++ is a mess and you could do something similar in D by
wrapping the reference in a value_semantics wrapper.

value_semantics!(value_semantics!(int[])[]) v5, v6;
v6 = v5; // Deep copy due to the value_semantics templated struct.

Unfortunately this doesn't solve the RAII problem: if
value_semantics!(T) deletes the contained reference in its destructor,
then it is no longer safe to embed a value_semantics!(T) variable in
memory managed by the garbage collector.

>> Would it remove the need for both classes and structs in the language,
> 
> It's not a bug, it's a feature.

Having both value types and reference types in the language is
(arguably) a feature.  Having no syntactic distinction between them at
the point of use is at best a misfeature.  Forcing programmers to change
their value types into reference types when they need polymorphism or
inheritance, even though safe polymorphic value types are possible, is
clearly a misfeature.

> [...] change my scanf calls [...]

Are you serious?


-- 
Rainer Deyke - rainerd at eldwood.com



More information about the Digitalmars-d mailing list