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

Re: return() in pointy blocks

Thread Previous | Thread Next
From:
=?ISO-8859-1?Q?=22TSa_=28Thomas_Sandla=DF=29=22?=
Date:
June 8, 2005 09:31
Subject:
Re: return() in pointy blocks
Message ID:
42A71D3D.8010300@orthogon.com
Piers Cawley wrote:
> "TSa (Thomas Sandlaß)" <Thomas.Sandlass@orthogon.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 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