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

Re: return() in pointy blocks

Thread Previous
From:
Ingo Blechschmidt
Date:
June 7, 2005 10:11
Subject:
Re: return() in pointy blocks
Message ID:
d84jvf$qkg$1@sea.gmane.org
Hi,

Matt Fowles wrote:
> On 6/7/05, Ingo Blechschmidt <iblech@web.de> wrote:
>>   sub foo (Code $code) {
>>     my $return_to_caller = -> $ret { return $ret };
>> 
>>     $code($return_to_caller);
>>     return 23;
>>   }
>> 
>>   sub bar (Code $return) { $return(42) }
>> 
>>   say foo &bar; # 42 or 23?
>> 
>> I think it should output 42, as the return() in the pointy
>> block $return_to_caller affects &foo, not the pointy block.
>> To leave a pointy block, one would have to use leave(), right?
> 
> I don't like this because the function bar is getting oddly
> prematurely halted.  If bar had read
> 
> sub bar(Code $moo) {
>     $moo(13);
>     save_the_world();
> }
> 
> it would not have gotten to save the world.  One might argue that $moo
> could throw an exception, but bar has a way to catch that.

yep. $moo(13) will never return.

But this is not specific to pointy blocks: Consider
  bar &return;

> It seems to me that what you are asking for has the potential to cause
> some vary large unexpected jumps down the stack.

Yep.

> so maybe this is just one of those things that one has to be ware of.

I think the reponsibility is at the user using &return or other
evil Codes (like, as in the example, -> $val { return $val }), not the
innocent subroutine programmer (&bar)) -- if you play with
continuations, you know what might happen. But they can be very useful,
too! :)


--Ingo

-- 
Linux, the choice of a GNU | Failure is not an option. It comes bundled 
generation on a dual AMD   | with your Microsoft product.  
Athlon!                    | 


Thread Previous


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