Re: Binding to a sub's return value

1 view
Skip to first unread message

Juerd

unread,
May 10, 2005, 3:57:40 PM5/10/05
to Joshua Gatcomb, perl6-l...@perl.org
Joshua Gatcomb skribis 2005-05-10 15:52 (-0400):
> sub some_routine {
> my $foo = 42;
> return $foo;
> }
> my $rv := some_routine();
> Should $rv be bound to $foo or to a copy of $foo? I ask because with
> state() and closures, it makes a difference since the value can
> change.

:= is the thing that implements subroutine arguments. Ask yourself the
same question with:

sub another_routine ($rv) {
...
}
another_routine(some_routine());

I'd expect $rv to be an alias to a copy of $foo's value, 42.

UNLESS some_routine is lvalue (which in this case it is not), in which
case, I'd expect $rv to be an alias to $foo itself

> sub some_rourtine {
> state $foo = 42;
> return $foo++;
> }

rvalue some_rourtine: copy 42

lvalue some_rourtine: error, ++ doesn't return an lvalue
(although prefix ++ probably could).


Juerd
--
http://convolution.nl/maak_juerd_blij.html
http://convolution.nl/make_juerd_happy.html
http://convolution.nl/gajigu_juerd_n.html

Joshua Gatcomb

unread,
May 10, 2005, 3:52:21 PM5/10/05
to perl6-l...@perl.org
I am wondering what the proper behavior of binding to a sub's return
value should be

sub some_routine {
my $foo = 42;
return $foo;
}
my $rv := some_routine();

Should $rv be bound to $foo or to a copy of $foo? I ask because with
state() and closures, it makes a difference since the value can
change.

sub some_rourtine {


state $foo = 42;
return $foo++;
}

My apologies if this has been previously discussed or is documented
somewhere. I am still playing catch up. Ok, ok - it's true - I am
looking for a little instant gratification.

Cheers,
Joshua Gatcomb
a.k.a. L~R

Aaron Sherman

unread,
May 10, 2005, 4:01:07 PM5/10/05
to Joshua Gatcomb, Perl6 Language List
On Tue, 2005-05-10 at 15:52, Joshua Gatcomb wrote:
> I am wondering what the proper behavior of binding to a sub's return
> value should be
>
> sub some_routine {
> my $foo = 42;
> return $foo;
> }
> my $rv := some_routine();
>
> Should $rv be bound to $foo or to a copy of $foo? I ask because with
> state() and closures, it makes a difference since the value can
> change.

Yes, so let's pose an easy, full example with a result we can point at:

my $x = 1;
my $y := (->{$x}).();
$x++;
say $y;

Ok, so clearly that prints 1 or 2 (the first person to say it's a
junction gets the mean look ;)

Some questions I'd add to yours:

* We discussed auto-closurizing := statements before, so when does
that happen vs. performing the operation and taking the value
exactly?
* If the above prints 2, then how do you return a value that is a
constant "safely"? Do you need to explicitly invoke a cloning
operation on it?
* How can I talk you out of it? ;-)

--
Aaron Sherman <a...@ajs.com>
Senior Systems Engineer and Toolsmith
"It's the sound of a satellite saying, 'get me down!'" -Shriekback


Brent 'Dax' Royal-Gordon

unread,
May 10, 2005, 9:34:46 PM5/10/05
to Juerd, Joshua Gatcomb, perl6-l...@perl.org
Juerd <ju...@convolution.nl> wrote:
> := is the thing that implements subroutine arguments. Ask yourself the
> same question with:
>
> sub another_routine ($rv) {
> ...
> }
> another_routine(some_routine());
>
> I'd expect $rv to be an alias to a copy of $foo's value, 42.

Really? Because the default parameter binding is constant reference,
last I checked.

I actually like that answer. It means that you can bind the return
value, but you can't mutate it, unless the function 'is rw'. (And
perhaps you could mark it as 'is copy' and 'is ref', too...)

--
Brent 'Dax' Royal-Gordon <br...@brentdax.com>
Perl and Parrot hacker

Reply all
Reply to author
Forward
0 new messages