Re: return() in pointy blocks
by Piers Cawley other posts by this author
Jun 8 2005 3:23AM messages near this date
Re: return() in pointy blocks
|
Re: return() in pointy blocks
Luke Palmer <lrpalmer@[...].com> writes:
> On 6/7/05, Matt Fowles <ubermatt@[...].com> wrote:
> > On 6/7/05, Ingo Blechschmidt <iblech@[...].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.
My preference is for:
Boo
Boo
Can't dereferene literal numeric literal 42 as a coderef.
Actually, my preference is for not writing such silly code in the first place,
but there you go.
Thread:
Piers Cawley
=22TSa_=28Thomas_Sandla=DF=29=22
|