Velocity Reviews > Re: comma operator

# Re: comma operator

Keith Thompson
Guest
Posts: n/a

 05-15-2009
Franken Sense <(E-Mail Removed)> writes:
> In Dread Ink, the Grave Hand of Franken Sense Did Inscribe:
> [misposted to clp.misc]
> [x-posted to clc; f-up set to clc]
>> I'm reading up on perl today and find this in perldoc perlop:
>>
>> Comma Operator
>>
>> Binary "," is the comma operator. In scalar context it
>> evaluates its left argument, throws that value away, then
>> evaluates its right argument and returns that value. This is
>> just like C's comma operator.
>>
>> // end excerpt
>>
>> What is C's comma operator?

It evaluates its left argument, throws that value away, then evaluates
its right argument and yields that value.

(Note that not all commas are comma operators; in particular, the
commas separating arguments in a function call or macro invocation are
not operators.)

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Franken Sense
Guest
Posts: n/a

 05-15-2009
In Dread Ink, the Grave Hand of Franken Sense Did Inscribe:

[misposted to clp.misc]
[x-posted to clc; f-up set to clc]
> I'm reading up on perl today and find this in perldoc perlop:
>
> Comma Operator
>
> Binary "," is the comma operator. In scalar context it evaluates its
> left argument, throws that value away, then evaluates its right
> argument
> and returns that value. This is just like C's comma operator.
>
> // end excerpt
>
> What is C's comma operator?

--
Frank

My father grew up in the Great Depression - his mother's.
~~ Al Franken

James Dow Allen
Guest
Posts: n/a

 05-15-2009
On May 15, 8:46*am, Keith Thompson <(E-Mail Removed)> wrote:
> (Note that not all commas are comma operators; in particular, the
> commas separating arguments in a function call or macro
> invocation are not operators.)

I've wondered if my rule is correct:
The comma in
A, B
is The Comma Operator if and only that is equivalent to
(A, B)

For example, only the second comma in
printf(A, (B, C))
is The Comma Operator because that is equivalent to
printf(A, ((B, C)))
but not
printf((A, (B, C)))

James

luserXtrog
Guest
Posts: n/a

 05-15-2009
On May 15, 2:49*am, James Dow Allen <(E-Mail Removed)> wrote:
> On May 15, 8:46*am, Keith Thompson <(E-Mail Removed)> wrote:
>
> > (Note that not all commas are comma operators; in particular, the
> > commas separating arguments in a function call or macro
> > invocation are not operators.)

>
> I've wondered if my rule is correct:
> The comma in
> * * *A, B
> is The Comma Operator if and only that is equivalent to
> * * *(A, B)
>
> For example, only the second comma in
> * * *printf(A, (B, C))
> is The Comma Operator because that is equivalent to
> * * *printf(A, ((B, C)))
> but not
> * * *printf((A, (B, C)))
>
> James

I find it easier to think of the comma as a semicolon without
a period. It terminates the expression without terminating the
statement.

--
lxt

Keith Thompson
Guest
Posts: n/a

 05-15-2009
luserXtrog <(E-Mail Removed)> writes:
[...]
> I find it easier to think of the comma as a semicolon without
> a period. It terminates the expression without terminating the
> statement.

Ah, but it doesn't terminate the expression; it only terminates a
subexpression.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Willem
Guest
Posts: n/a

 05-15-2009
Richard Heathfield wrote:
) Yes, it does. An expression is "a sequence of operators and operands
) that specifies computation of a value, or that designates an object
) or a function, or that generates side effects, or that performs a
) combination thereof". A comma operator always terminates such a
) sequence.

So, what would you call this then:

((1, 2, 3) + (4, 5, 6))

SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT

Keith Thompson
Guest
Posts: n/a

 05-15-2009
Richard Heathfield <(E-Mail Removed)> writes:
> Keith Thompson said:
>> luserXtrog <(E-Mail Removed)> writes:
>> [...]
>>> I find it easier to think of the comma as a semicolon without
>>> a period. It terminates the expression without terminating the
>>> statement.

>>
>> Ah, but it doesn't terminate the expression; it only terminates a
>> subexpression.

>
> Yes, it does. An expression is "a sequence of operators and operands
> that specifies computation of a value, or that designates an object
> or a function, or that generates side effects, or that performs a
> combination thereof". A comma operator always terminates such a
> sequence.

You're right, of course. (The standard's definition of "expression"
is broken, but that's a separate point.)

Incidentally, that's true only because the comma operator has a lower
precedence than any other operator.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Keith Thompson
Guest
Posts: n/a

 05-15-2009
Keith Thompson <(E-Mail Removed)> writes:
> Richard Heathfield <(E-Mail Removed)> writes:
>> Keith Thompson said:
>>> luserXtrog <(E-Mail Removed)> writes:
>>> [...]
>>>> I find it easier to think of the comma as a semicolon without
>>>> a period. It terminates the expression without terminating the
>>>> statement.
>>>
>>> Ah, but it doesn't terminate the expression; it only terminates a
>>> subexpression.

>>
>> Yes, it does. An expression is "a sequence of operators and operands
>> that specifies computation of a value, or that designates an object
>> or a function, or that generates side effects, or that performs a
>> combination thereof". A comma operator always terminates such a
>> sequence.

>
> You're right, of course. (The standard's definition of "expression"
> is broken, but that's a separate point.)
>
> Incidentally, that's true only because the comma operator has a lower
> precedence than any other operator.

I may be wrong about that. I'll think about it when I have more time.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Keith Thompson
Guest
Posts: n/a

 05-15-2009
Richard Heathfield <(E-Mail Removed)> writes:
> Willem said:
>> Richard Heathfield wrote:
>> ) Yes, it does. An expression is "a sequence of operators and
>> operands ) that specifies computation of a value, or that
>> designates an object ) or a function, or that generates side
>> effects, or that performs a ) combination thereof". A comma
>> operator always terminates such a ) sequence.
>>
>> So, what would you call this then:
>>
>> ((1, 2, 3) + (4, 5, 6))

>
> I would call 1 an expression, which is terminated by a comma
> operator.

[...]

I would call 1 an expression. But I wouldn't call it "a sequence of
operators and operands". Here it's an operand of the comma operator,
but in isolation:

1;

there are no operators, and therefore 1 is not an operand.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Willem
Guest
Posts: n/a

 05-15-2009
Richard Heathfield wrote:
) Willem said:
)> So, what would you call this then:
)>
)> ((1, 2, 3) + (4, 5, 6))
)
) I would call 1 an expression, which is terminated by a comma
) operator.
) I would call 2 an expression, which is terminated by a comma
) operator.
) I would call 4 an expression, which is terminated by a comma
) operator.
) I would call 5 an expression, which is terminated by a comma
) operator.
)
) I would call the whole thing an expression, too.
)
) What would you call these expressions?

Subexpressions, obviously. They're all parts of the whole thing,
which is an expression.

What I wanted to know is what you call the operands of the '+' operator.

SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT