View mode: basic / threaded / horizontal-split · Log in · Help
October 30, 2010
why is this an invalid code?
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
October 30, 2010
Re: why is this an invalid code?
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
October 30, 2010
Re: why is this an invalid code?
This does seem like a strange omission. But thanks for the answer!

thanate
October 30, 2010
Re: why is this an invalid code?
thanate:

> This does seem like a strange omission. But thanks for the answer!

Indeed, some methods are missing in the built-in associative arrays. I think Andrei likes the idea of adding few more methods (dup too) to those AAs, but no one has found the time to implement them yet.

Bye,
bearophile
Top | Discussion index | About this forum | D home