Fwd from Luke -- he's adopted a retarded MUA.

--- Luke Palmer <[EMAIL PROTECTED]> wrote:
> Date: Mon, 11 Aug 2003 21:22:05 -0600
> From: Luke Palmer <[EMAIL PROTECTED]>
> Subject: Re: Perl 6's for() signature
> 
> Austin Hastings writes:
> > > And you can't do that because the loop has no way of knowing that
> your
> > > lexicals are referring to &block's parameters.
> > 
> > Which begs the question:
> > 
> >   my &blk = -> $a, $b {...};
> > 
> >   for (@list) &blk;
> > 
> > What happens?
> 
> Exactly the same thing as:
> 
>     for @list -> $a, $b {...};
> 
> You get a yada-yada-yada exception :-P
> 
> Seriously, think of them like this:
> 
>     my &blk = sub ($a, $b) {...};
>     for @list, &blk;
> 
> and
> 
>     for @list, sub ($a, $b) {...};
> 
> > (IOW, how do we map locals/parameters into block vars?)
> 
> That is, block vars I<are> parameters.
> 
> > Or, given
> > 
> >   my &blk = -> $a {...};
> > 
> >   loop (my ($x, $max) = (0, -1);
> >         $x < $num_elts;
> >         $x++, $max = max($max, $x)),
> >      &blk;
> > 
> > What happens?
> 
> That's a different story.  You get a "not enough arguments to call"
> at runtime.
> That's because C<loop> doesn't map parameters; instead, it repeatedly
> manipulates
> lexicals.  You'd have to write:
> 
>     loop (...)
>         &blk.assuming(a => $x);
> 
> Or as I said before,
> 
>     loop (...) { blk($x) }
> 
> 
> > (IOW, in a "target-rich" environment, how do we know what to bind?)
> > 
> > > Then again, C<for>
> > > could be made to do that using named parameters, but I don't
> think it
> > > will (there are some nasty traps with variables in outer scopes).
>  It's
> > > easy enough to say:
> > >
> > >     loop (my ($a, $b) = (0, 1); $a < $b; ++$a) { block($a, $b) }
> > >
> > 
> > Or maybe we have to say
> > 
> >     loop (...) -> $a &blk;
> > 
> > But that's nasty.
> 
> Yeah, don't think so.  Plus, the block to C<loop> can't take any
> parameters,
> because C<loop> doesn't know what to give it.
> 
> In summary:
> 
> C<for> takes a list and a parameterized block &blk, calling blk
> repeatedly
> with the elements of the list as arguments.  C<loop> (and C<while>
> and friends)
> take a closure with no parameters, and mess with variables in such a
> way
> that they look like "loop variables".  They have no idea what to pass
> the
> block as parameters.
> 
> Hope this cleared some things up.
> 
> Luke
> 
> > 
> > 
> > > Now on to your argument :-)
> > >
> > > > We're really just pulling the block vars out so we can tweak
> them.
> > >
> > > Yeah, sortof.
> > 
> > ...
> > 
> > > It's a beginner trap, so Larry changed it to follow intuition. 
> It can
> > > be useful in a couple places, and it never really gets in your
> way.  So,
> > > poof, no more magic scopes.
> > 
> > Hooray for newbies. And that's a good point.
> > 
> > I'm still curious about the binding rules, though.
> > 
> > =Austin
> > 

Reply via email to