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

Re: return() in pointy blocks

Thread Previous | Thread Next
From:
Luke Palmer
Date:
June 7, 2005 14:07
Subject:
Re: return() in pointy blocks
Message ID:
7ca3f0160506071407786d4fe5@mail.gmail.com
On 6/7/05, Matt Fowles <ubermatt@gmail.com> wrote:
> On 6/7/05, Ingo Blechschmidt <iblech@web.de> wrote:
> > Hi,
> >
> >   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. 

Then let's put it this way:

   sub foo () {
       for 0..10 { 
           when 6 { return 42 }
       }
       return 26;
   }

And if that didn't do it, then let's write it equivalently as:

   sub foo () {
       &map(-> $_ { return 42 }, 0..10);
       return 26;
   }

Do you see why the return binds to the sub rather than the pointy now?

Also, we're going to be avoiding the return continuation problem with:

   sub foo() { 
       return -> { return 42 };
   }
   
   my $code = foo();
   say "Boo!";
   $code();

Says not:

   Boo
   Boo
   Boo
   ...

But:

   Boo
   Can't return from subroutine that already returned at <eval> line 2.

Luke

Thread Previous | Thread Next


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