www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - expression

reply Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
what is the purpose of the expression grammar

Expression:
	AssignExpression
	AssignExpression , Expression

?

(emphasis on the comma)
May 29 2009
parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Ellery Newcomer wrote:
 what is the purpose of the expression grammar
 
 Expression:
 	AssignExpression
 	AssignExpression , Expression
 
 ?
 
 (emphasis on the comma)
To allow two expressions separated by a comma to be another expression. This is only useful if the expression before the last comma has side-effects of course, since its value isn't used. Such an expression returns the result of its right-hand side. For example: (foo(), bar()) is an expression that calls foo(), then calls bar() and evaluates to the return value of bar(). This is mostly used in the increment clause of a for loop, like this: for (int i = 0; i < max; i++, someOtherCounter++)
May 30 2009
parent bearophile <bearophileHUGS lycos.com> writes:
Frits van Bommel:
 To allow two expressions separated by a comma to be another expression. This
is 
 only useful if the expression before the last comma has side-effects of
course, 
 since its value isn't used. Such an expression returns the result of its 
 right-hand side.
 For example: (foo(), bar()) is an expression that calls foo(), then calls
bar() 
 and evaluates to the return value of bar().
 This is mostly used in the increment clause of a for loop, like this:
Outside for loops is a bug-prone and not much useful feature. (In python the comma is more useful and safer). Bye, bearophile
May 30 2009