Em Dom, 2009-06-14 às 15:53 -0500, John M. Dlugosz escreveu:
> In Perl 6, the default parameter passing is to make a read-only alias 
> for the caller's lvalue.  This means that the function may not change 
> the caller's variable, but must track changes to it made by other means.
> What is the point?
> It is a contrivance to illustrate how the variable can be changed by 
> other means, and requires a global variable, the same variable passed as 
> two different parameters, or the variable and a closure that affects the 
> variable be passed.

Actually, it only looks complicated while you think only on the callee
side. Because when you take the caller side, you'll note that it builds
a capture to send to the call, and the capture is always a reference, so
the signature just makes sure that references becomes read-only. To
illustrate:

 my $a = 1;
 foo($a);

In this case, the capture sent must contain a direct reference to the
scalar held in '$a', so both signatures with "is ref" or signatures with
"is copy" can work.

So, if foo has the signature

 sub foo($a is ref) {...}

it will be able to change the scalar outside &foo. If it is

 sub foo($a) {...}

It will be a read-only access to that scalar

 sub foo($a is rw) {...}

Works almost like "is ref", but encloses immutables into a container in
order to always provide rw semantics.

 sub foo($a is copy) {...}

Is the completely opposite to "is ref", copying the actual value to a
new container.

So, it is not at all complicated, it's just oriented to the Capture, and
the capture provides semantics to the call that are not present in any
other language I'm aware of.

daniel

Reply via email to