News for hackers. Stuff that matters.
Re:How Scala Changed My Programming Style by silentOpen on Wednesday April 29, 2009 @12:46PM (#585356)
Uh? Maybe with a brain-damaged compiler?
Re:How Scala Changed My Programming Style by shrughes on Wednesday April 29, 2009 @12:55PM (#585376)
Or with C++. There's no reason to adopt i++ sometimes and ++i other times depending on what the type is.
Re:How Scala Changed My Programming Style by silentOpen on Wednesday April 29, 2009 @03:44PM (#585715)
C++ copies values differently (or at all) for different types under prefix increment vs. postfix increment? Seriously?
Re:How Scala Changed My Programming Style by shrughes on Wednesday April 29, 2009 @04:02PM (#585749)
Yes. When overloading prefix and postfix increment, there are two functions to write, and the postfix version returns a copy of what you've incremented. It's impossible for a compiler to optimize this away in the general situation.
Re:How Scala Changed My Programming Style by dkarl on Wednesday April 29, 2009 @06:38PM (#586025)
There's no difference between C and C++, except that in C++, you can define prefix and postfix operators on user-defined types. The two are defined separately because C++ was created for people who are really finicky about performance. (For some reason some people are really bothered by the possibility that you could provide really insane definitions, as if it wasn't already possible to create insanely named functions and methods....)

Anyway, even in C, ++i and i++ change the value of i and evaluate to a value. So there are two values: the new value of i, and the value of the expression. In the case of prefix ++, the two values are the same. In the case of postfix ++, the two values are different. If you ignore the value of the expression, then ++i and i++ can be used interchangeably. The compiler will probably figure this out and produce the same code.