develooper Front page | perl.perl6.language | Postings from June 2006

lvalue functions and lvalue parameters

Thread Next
Daniel Hulme
June 20, 2006 02:59
lvalue functions and lvalue parameters
Message ID:
I've just thought of an interesting interaction with lvalue functions
and call by foo. What if I want to write, say, an lvalue ?? !! function

sub cond(Bool $c, $a, $b) is rw {
  if $c return $a else return $b;

Will this fail because $a and $b are not rw? If so, will it fail at run-
or compile-time? What about this:

sub cond(Bool $c, $a is copy, $b is copy) is rw {
  if $c return $a else return $b;

Is it allowed, and if so is the behaviour to return a writeable copy of
$a or $b? I imagine that in any case

sub cond(Bool $c, $a is rw, $b is rw) is rw {
  if $c return $a else return $b;

will do what I want. I suppose the issue comes down to the scope of
modifiers like rw. In the first example, $a and $b are constant in the
scope of the function, but they aren't written in the scope of the
function. They are then passed to the caller which may then try to write
them, but will they remember that they are rw in the caller's scope?
Furthermore, what if they aren't rw in the caller's scope?

my Int $a is constant = 0;
my Int $b is constant = 0;
(cond(True, $a,$b))++;

What goes on here, for any of the cond definitions? Do any of them
result in breaking the constancy of $a? I'm not sure, but I think my
vote is for contestant number one to fail at compile-time, number two to
generate new, writeable copies, and number three to give the returned
lvalue the same writeability as its caller.

Come to that, what is the behaviour of constant variables being rw'ed in
parameter lists?

sub rwify($a is rw) { $a += 19; }
my Int $a is constant = 23;
rwify $a;
say "The meaning of life is $a";

I would imagine it should fail on the function call, but I'm open to
other suggestions.

Humpty  Dumpty  sat on  the  wall,  Humpty  Dumpty  had  a  great  fall,
All the King's horses and all the King's men |
Couldn't put Humpty together again.          |   powered by cat and ^d  
Perhaps they shouldn't have given the horses the first go.

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About