[DGD]returning copy() of arrays, objects

Neil McBride neil at ekit-inc.com
Mon Jan 31 03:31:48 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 ;
> }
> 

When passing a mapping to a local function, the actual mapping is
passed, as opposed to a copy.  Thus, any changes made to the mapping
within the called function will affect the mapping within the calling
function.  To stop changes affecting the calling functions mapping, you
pass a copy of the mapping in the first place (ie, a[..] instead of just
a).  I don't believe arrays behave this way but could be wrong.  

Perhaps this behaviour is the same when returning a value, but
considering the function finishes after returning a value, perhaps not
as there would be no value in it unless dealing with global values, at
which point all changes take affect anyway.  

Perhaps whoever wrote that code thought the behaviour was as I
described, or perhaps they were just having problems and the above code
worked past it.

I know there's a lot of 'perhaps' in that, but I don't see any possible
reason for it.  Is the person who wrote Melville subscribed? Maybe they
can explain =)

Cheers,

Neil.

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



More information about the DGD mailing list