[DGD]returning copy() of arrays, objects

Neil McBride neil at ekit-inc.com
Mon Jan 31 03:40:00 CET 2000


Frantisek Fuka wrote:
> 
> While studying Melville Mudlib I found out that the functions which
> return objects, arrays or mappings return "copy (result)" instead of
> just "result", where copy() is defined in auto.c as:
> 
> nomask mixed copy (mixed a) {
>     mixed b ;
>     if (typeof(a)==T_ARRAY || typeof(a)==T_MAPPING) {
>         b = a[..] ;
>     } else {
>         b = a ;
>     }
>     return b ;
> }
> 

Typical, just after I hit send, I realised what it may be.  Given the
nature of passing mappings in local objects, I'd say this function is
used as a proxy of sort between two functions.  So, when passing a
mapping to a function and you only want a copy of the mapping sent (thus
no changes to the original mapping), this could be used as follows: -

void some_fun() {
  mapping foo;

  foo = ([ "A" : "a" ]);

  /* Any changes in call_another_func to foo will NOT affect
     the mapping in this function in the following call */       
  call_another_func(copy(foo));

  /* Any changes in call_another_func to foo WILL affect
     the mapping in this function in the following call */
  call_another_func(foo);
}

I don't know why you simply wouldn't use call_another_func(foo[..])
though, unless you don't know what type of variable you're using in the
calling function.  If you're dealing with mixed, the copy function would
come in handy.

Neil.

List config page:  http://list.imaginary.com/mailman/listinfo/dgd



More information about the DGD mailing list