On Wed, 28 Aug 2002, Thom Boyer wrote:
: Damian Conway wrote:
: > Any subroutine/function like C<if> that has a signature (parameter list)
: > that ends in a C<&sub> argument can be parsed without the trailing
: > semicolon. So C<if>'s signature is:
: > 
: >         sub if (bool $condition, &block);
: 
: So what does the signature for C<while> look like? I've been wondering about
: this for a long time, and I've searched the Apocalypses and the
: perl6-language archive for an answer, but I've had no success.
: 
: It seems like C<while>'s signature might be something like one of these:
: 
:       sub while (bool $test, &body);
:       sub while (&test, &body);
: 
: But neither of these really works. 

That's correct.  Maybe something like

      sub while (&test is expr, &body);

But that would be shorthand for something more general--see below.

: The first would imply that the test is evaluated only once (and that once is
: before 'sub while' is even called). That'd be useless.
: 
: The second would allow multiple evaluations of the test condition (since
: it's a closure). But it seems that it would also require the test expression
: to have curly braces around it. And possibly a comma between the test-block
: and the body-block. That'd be ugly.

Maybe we could have something like:

     sub while (&test is rx/<expr>/, &body);

or some such.  That probably isn't sufficient to pick <expr> out of Perl's
grammar rather than the current lexical scope.

: I can create a hypothetical "bareblock" rule that says:
: 
:       When an argument's declaration contains an ampersand sigil,
:       then you can pass an "expression block" (i.e., a simple 
:       expression w/o surrounding curlies) to that argument.
: 
: Is there such a rule for Perl 6? 

Not at the moment.  It'd be pure obfuscation if people did that where
curlies *are* expected.  I still want the curlies required on an "else",
for instance.

: On the positive side, this would be an reasonable generalization of the Perl
: 5 handling of expressions given to map or grep.

I don't particularly like the old map and grep syntax.

: On the negative side, this
: rule makes it impossible to have such arguments fulfilled by evaluating an
: expression that returns the desired closure (i.e., the expression you type
: as an argument isn't intended to be the block you pass, but rather it is
: intended to generate the block you want to pass).

Well, we could make the same sort of rule that we (eventually) did
for bare blocks--if you want to return a closure in that circumstance
you'd have to use "sub' (or "return", in the case of a bare block).

: In summary: assuming Perl 6 allows user-defined while-ish structures, how
: would it be done?

I think the secret is to allow easy attachment of regex rules to sub
and parameter declarations.  There's little point in re-inventing
regex syntax using declarations.  The whole point of making Perl 6
parse itself with regexes is to make this sort of stuff easy.

Larry

Reply via email to