develooper Front page | perl.perl6.language | Postings from January 2004

Re: Comma Operator

Thread Previous | Thread Next
Larry Wall
January 22, 2004 10:30
Re: Comma Operator
Message ID:
On Wed, Jan 21, 2004 at 08:51:33PM -0500, Joe Gottman wrote:
: Great, so
:     $x = foo(), bar();
: means the same thing as
:     $x = ( foo(), bar() );

No, we haven't changed the relative precedence of assignment and comma.
I've been tempted to, but I always come back to liking the parens
for visual psychological purposes.  Plus changing the precedence
would break

    loop $i = 0, $j = 0; $x[$i,$j]; $i++, $j++ { ... }

:  Is the optimizer going to be smart enough so that given the expression
:     $x = (foo(), bar(), glarch())[-1];
: Perl6 won't have to construct a three-element array just to return the last
: element?

It's interesting to me that you assume there would be a three element
array there.  If the ()[] works like it does in Perl 5, the stuff inside
the parens is always evaluated in list context.  Which means that foo(),
bar(), and glarch() can any of them return 0 or more elements.  There's
no guarantee that the final value doesn't come from foo() or bar().

Now, in Perl 6, slices are distinguished from single subscripts only by
the form of the subscript, not by any sigil.  So we can know for a fact
that [-1] is not a slice.  But that doesn't mean we can necessarily
assume that the list in parens wants to evaluate its args in scalar
context.  Maybe it does, and maybe it doesn't.

That doesn't mean that we have to put the C comma operator back
in though.  It might just mean that the default is wrong on ()[].
Suppose we say that

    (foo(), bar(), glarch())[-1]

by default evaluates its arguments in scalar context.  To get the Perl 5
behavior, you'd merely have to use a splat to put the list into list

    (* foo(), bar(), glarch())[-1]

Then (...)[] would become the standard way of producing a list of
things evaluated in scalar context.  Alternately, if you don't like
the splat list, you can always say

    [foo(), bar(), glarch()][-1]

which does the same thing.

Given all that, I think we can say that, yes, the compiler can
optimize foo() and bar() to know they're running in void context.
I'd generalize that to say that any unselected element of a scalar
list can be evaluated in a void context, whether the subscript is -1
or 0 or any constant slice.


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About