why is this an invalid code?

Jonathan M Davis jmdavisProg at gmx.com
Sat Oct 30 12:54:15 PDT 2010


On Saturday 30 October 2010 12:12:48 thanate wrote:
> Hi,
> 
> dmd v2.050 give the following error:
> 
> hello.d(10): Error: undefined identifier module hello.dup
> 
> for the following code (hello.d):
> ---------------------
> import std.stdio;
> import std.array;
> import std.string;
> 
> void main()
> {
>   uint[string] s;
>   s["a"]=1;
>   s["b"]=2;
>   uint[string] r = s.dup;
>   r["b"]=3;
>   writeln(r["b"]," ",s["b"]);
> }
> ---------------------
> 
> Could anyone explain why should this be an error?
> 
> Thanks!
> thanate

Easy. s is an associate array, not an array, and for whatever reason, 
associative arrays don't have a dup property: 
http://www.digitalmars.com/d/2.0/hash-map.html

I believe that if you want a deep copy, you're going to have to create a new one 
and then copy all of the elements over in a loop.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list