develooper Front page | perl.perl6.language | Postings from March 2005

return of copies vs references

Thread Next
From:
Darren Duncan
Date:
March 16, 2005 21:49
Subject:
return of copies vs references
Message ID:
p06210200be5ec7fe43dd@[192.168.1.102]
I need some clarification on the semantics of subroutine or method 
return statements, regarding whether copies or references are 
returned.  It will help me in my p6ification of p5 code.

Say I had a class with 3 private attributes, named [$:foo, @:bar, 
%:baz], and I was making an explicit accessor for returning the full 
values of each.

Take these 3 example method statements:

   return $self.foo;
   return $self.bar;
   return $self.baz;

For each of the above cases, is a copy of or a reference to the 
attribute returned?  For each, will the calling code be able to 
modify $obj's attributes by modifying the return values, or not?

Going further, what is the exact syntax for each type of attribute to 
specify whether a copy or a reference is returned?

In Perl 5, with the latter two, the difference was a "return $bar" vs 
"return [@{$bar}]" for reference vs copy.  I would like that Perl 6 
is also at least as clearly disambiguated.

Note that specifying this in the attribute definition isn't 
appropriate, since an attribute could just as easily be an array of 
arrays, or hash of hashes, and I am returning an inner array or hash 
that I either do or don't want to be modifiable by calling code.

Separate question, just to confirm, I assume that plain '=' always does a copy?

Thank you for any clarification.

-- Darren Duncan

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About