Array type conversion

Walter Bright newshound1 at digitalmars.com
Sat Apr 28 14:22:41 PDT 2007


Mark Burnett wrote:
> Certainly making a copy prevents this problem, but what I'm really curious about is the motivation for including these imlicit conversions (they are mentioned specifically on the Array description page on digitalmars.com).
> 
> They're not safe in general,

Why not?

> so I imainge library designers will end up using their own user-defined array/vector type just as in c++, so that such errors are caught at compile time.  This seems to limit the usefulness of these built in array types.  Sure they're bounds-safe, but they don't seem 100% type-safe.
> 
> For example:
> 
> std::vector<derived> ad;
> std::vector<base> ab;
> 
> ab = ad; // c++ compiler error
> 
> A compiler error is more what I would expect.  Treating a container of derived as a container of base is an error.

But that isn't what is happening with D. base[] is an array of 
*references* to base, so the slicing problem one has in C++ is not 
possible in D.

> 
> Not to speculate too much on the reasons, but is it just too much overhead for a built-in type to disallow this behavior?  Am I underestimating the usefulness of these built-in arrays in library design?
> 
> Mark
> 
> PS Thanks for your replies so far ;)



More information about the Digitalmars-d mailing list