On Mon, Apr 19, 2004 at 04:48:05AM -0600, Luke Palmer wrote:
: Trey Harris writes:
: > Can anyone explain the rules of placeholder attachment?  i.e., in the
: > example in Perl6::Placeholder's manpage,
: > 
: >   grep { $data{$^value} } 1..10;
: > 
: > C<$^value> is clearly intended to attach to the outer closure C<{
: > $data{$^value} }>, not the inner closure C<{$^value}>.  But how does the
: > compiler know?  What is the general rule?
: 
: This is a tough question, one to which I don't know the answer.  I'll
: do something different for a change and speculate :-)

Hey, I never speculate.  ;-)

: In your first example, it does what you mean because the hash subscript
: isn't a closure.  Curlies are always closures, except when they're not
: (to be specific, in hash subscripts and hash constructors).

Yes, that's the basic rule.  In Perl 5 the curlies are (potentially)
blocks, though the optimizer throws away the block entry and exit
when it thinks it can.  In Perl 6 we'll just say that those aren't
blocks.  If you really want a block inside a subscript, you can always
use do {}.  (But merely putting semicolons inside a subscript turns
it into a multidimensional subscript, not a block.)

: > It's easy to just say "don't nest placeholder-using closures," but that
: > doesn't seem workable in practice since every block is a closure, unless
: > placeholders are forbidden from all but the most trivial cases.  Absurdly
: > trivial, it seems.  How about
: > 
: >   $sub = { if $^a { $^b = $^a } };
: 
: I want this to work.  It could look at C<if>'s signature and see that
: the closure it is expecting wants arguments, and since it doesn't, it
: knows that they belong outside.  But that doesn't generalize.

I don't think I want that to work.

: I think a better solution would be to associate all placeholders with
: the outermost closure that introduced a placeholder.  For example:
: 
:     $sub = { { $^a + $^b } };
: 
: Would bind them both to the inner one, while:
: 
:     $sub = { $^a; { $^a + $^b } };
: 
: Would bind them both to the outer one.

This is the sort of twisty thinking that some people can keep straight
and some people can't.  That's why we simplified the list of rules
from the original placeholder RFC, after all.

: Since placeholders are meant for
: small scopes, this seems a good heuristic.  That second example was
: obviously a hack to get it to work right.  The clean way to do that
: would be:
: 
:     $sub = -> $a, $b { { $a + $b } };

Yup.

Larry

Reply via email to