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 [EMAIL PROTECTED]" 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

Reply via email to