Tuesday, December 23, 2003
Comma operator
The comma operator in C/C++ is an odd bird. It takes two operands (a, b), evaluates the first, discards its value, evaluates the second and returns it's value as the result of the expression. Comma separated operands can be chained together, evaluated in left-to-right sequence with the right-most value yielding the result of the expression. It's all very Lisp-like but seldom used in C/C++ code except in for statements and macros. Note: the commas used to separate the arguments in a method call, or the elements in an array are not comma operators.
Here's something I hadn't thought about before: Java does not support the comma operator. It allows comma separated initialization inside a for statement (e.g. (for i = 0, j = 0; i < foo.size(); i++) { ... } but doesn't consider the comma in this case to be an operator. Since I rarely used commas in C/C++, I didn't notice the lack of support in Java. It turns out that C# doesn't support the comma operator either. As with Java, the C# for statement supports comma separated intializers but doesn't consider the comma to be an operator.
Now you may ask why I was even thinking about an arcane language feature like the comma operator. Well, besides being a language geek I was thinking about multiple return values. Java, C# and most compiled languages don't support them and I think that's a shame. I have a blog entry in progress discussing this but it'll have to wait until another day.
Here's something I hadn't thought about before: Java does not support the comma operator. It allows comma separated initialization inside a for statement (e.g. (for i = 0, j = 0; i < foo.size(); i++) { ... } but doesn't consider the comma in this case to be an operator. Since I rarely used commas in C/C++, I didn't notice the lack of support in Java. It turns out that C# doesn't support the comma operator either. As with Java, the C# for statement supports comma separated intializers but doesn't consider the comma to be an operator.
Now you may ask why I was even thinking about an arcane language feature like the comma operator. Well, besides being a language geek I was thinking about multiple return values. Java, C# and most compiled languages don't support them and I think that's a shame. I have a blog entry in progress discussing this but it'll have to wait until another day.
RSS 0.92 Feed