ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> perl6-language
perl6-language
Re: return() in pointy blocks
by =22TSa_=28Thomas_Sandla=DF=29=22 other posts by this author
Jun 8 2005 9:31AM messages near this date
Re: return() in pointy blocks | Re: return() in pointy blocks
Piers Cawley wrote:
>  "TSa (Thomas Sandlaß)" <Thomas.Sandlass@[...].com> writes:
>  
>  
> >Piers Cawley wrote:
> >
> >>My preference is for:
> >>    Boo
> >>    Boo
> >>    Can't dereferene literal numeric literal 42 as a coderef.
> >
> >How do you reach the second 'Boo'? Iff -> does not create a Sub
> >but a Block instance then Luke's code can be interpreted as a
> >much smarter version of
>  
>  
>  I really wish you'd quote in more detail, it makes it a real PITA to go back
>  and find the explanatory code.  

Sorry, but I try to quote only the text that I'm really referring to,
not the transitive closure of the thread at that point. If I cut
too much here, I apologize.


>      sub foo () {
>        return -> { return 42 }
>      }

What I do not understand right now is, how the first
return is handled. As Luke pointed out, the inner return
inevitably makes the current invocation of &foo return.
But you argue, that ->  suspends the current invocation
of foo and returns it as continuation. How does that fit
the pointy sub of a for loop? Is the surrounding sub
suspended as well? And then one could continue right there?

sub forfoo ()
{
    for 0..9 ->  $x
    {
       return $x;
    }
}

my $y = forfoo(); # suspends forfoo in first loop?

say $y; # prints 0, and suspends forfoo in second loop

say **$y; # prints the remaining invocations of &forfoo?


I figure that the implementation is actually straight
forward. The pointy sub catches the return exception
and handles it by suspending &forfoo and returning the
resulting continuation.


>      my $code =   foo();
>      #          ^--- continuations points to the RHS of the assignment
>      say "Boo!";
>      $code();
>  
>  So, this is what happens.
>  
>  1. foo() returns a coderef to the RHS of the assignment.
>  2. The coderef gets assigned to $code.
>  3. say "Boo!"
>  4. We invoke the coderef, which returns 14 to continuation which was current
>     when it was created.
>  5. That means 42 gets returned to the RHS of the assignment
>  6. say "Boo!"
>  7. Try to invoke the literal 42.

Hmm, I see in both cases an MMD to &postfix:<( )>  with
the then current value of $code as invocant. In the
first run the target is &postfix:<( )> :( Ref of Block :)
and in the second case it's &postfix:<( )> :( Int :).
If the latter is defined to die, so be it. I opt for
just returning the value. Which produces the fallout that

$y = 42();

Is first of all valid syntax, and dispatches at runtime
unless the compiler can proof at compile time that there
can't possibly be an implementation at runtime. Thus one
could write:

multi sub *postfix:<( )>  ( 42 ) # or ( Int $x where $x eq '42' )
{
    return 66; # == hex 42
}

A pragma like 'use hexliterals' might actually implement a
general version of this idea.
-- 
TSa (Thomas Sandlaß)
Thread:
Piers Cawley
=22TSa_=28Thomas_Sandla=DF=29=22

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved