> -----Original Message-----
> From: Larry Wall [mailto:[EMAIL PROTECTED]
> Sent: Friday, August 18, 2006 11:47 AM
> To: Perl6 Language List
> Subject: Re: NEXT and the general loop statement
> 
> On Fri, Aug 18, 2006 at 01:44:35AM -0600, Luke Palmer wrote:
> : On 8/17/06, Jonathan Scott Duff <[EMAIL PROTECTED]> wrote:
> : >Depends on when it fires I guess. Your example might be equivalent to
> : >this perl5ish:
> : >
> : >    while (1) {
> : >        $num = rand;
> : >        print $num;
> : >        last if $num < 0.9;
> : >        print ",";              # NEXT
> : >    }
> : >    print "\n";                 # LAST
> :
> : Which, incidentally, relates back to the discussion at hand.  If this
> : were the case, though, the state of the variables (in three-arg for
> : loops and side-effecty while loops) would seem to reflect the state of
> : the next iteration from the rest of the code.  It would also
> : (obviously) fire after user input for eg. a for =<> loop.
> :
> : So, unless the next paragraph is feasible, I think NEXT ought to be
> : equivalent to LEAVE, perhaps with the exception of exceptional
> : circumstances.
> 
> I think it is equivalent to LEAVE, unless you've explicitly opted out
> with "last" or an exception.
> 
> : But I was talking about hypotheticalization.  That is, unless I am
> : mistaken we have temp {} and let {} using UNDO blocks (which are given
> : default definitions where possible).  So perhaps we could use that
> : feature to provide a NEXT trait which actually executes only if there
> : is a next iteration, while giving the block the impression that it is
> : executing in the previous iteration.  This, of course, would not work
> : for loops whose conditions have irreversible side-effects.
> :
> : Overall, it might be feasible in some circumstances, but it may not be
> : worth it.  Its implementation (and usage caveats) are quite complex
> : for a relatively minor convenience feature.  For a user to implement
> : its effect by himself, using the extended knowledge he has of the loop
> : semantics, would probably not take more than four extra lines in the
> : worst case.
> 
> It would mean that we can't use NEXT to replace Perl 5's "continue"
> blocks, for instance, since those are required to run before the
> while loop's condition is tested.  Which also basically means we
> couldn't rewrite loop (;;) using NEXT.  So I think NEXT should just
> be equivalent to LEAVE from the viewpoint of conditionals outside
> the loop body.  You have to use "last" to do better.
> 

   One alternative that would work well in many cases would be to define a
REENTER block, which would be called at the beginning of any loop iteration
after the first one. This would be much easier to implement that the idea of
NEXT blocks being mutually exclusive to LAST blocks, because it depends only
on what happened in the past, not the future.  Luke's example could then be
implemented as

    for @objs {
        .print;
        REENTER { print ", " }
        LAST { print "\n" }
    }

In a language that has both if and unless, it makes sense to have a block
that means not FIRST. I'm not sure about the name; besides REENTER, other
possible names would be SUBSEQUENT or NOTFIRST.

Joe Gottman

Reply via email to