www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - why is this an invalid code?

reply thanate <nospam spam.com> writes:
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
Oct 30 2010
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
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
Oct 30 2010
parent reply thanate <nospam spam.com> writes:
This does seem like a strange omission. But thanks for the answer!

thanate
Oct 30 2010
parent bearophile <bearophileHUGS lycos.com> writes:
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
Oct 30 2010