C programmers: beware "$var++" in torquescript
by Orion Elenzil · in Torque Game Engine · 04/12/2006 (11:10 am) · 9 replies
In The Game Programmer's Guide To Torque, Ed Maurina demonstrates but doesn't comment on the fact that torquescript's implementation of the postfix increment/decrement operators is very different than C/C++'s.
in C++, the postfix operator "++" means "use the variable with it's current value, and then increment it."
in torquescript, the same syntax means "increment the variable first, and then use it".
this is radically different and has screwed me up any number of times.
so now you know !
c++ example
torquescript example
in C++, the postfix operator "++" means "use the variable with it's current value, and then increment it."
in torquescript, the same syntax means "increment the variable first, and then use it".
this is radically different and has screwed me up any number of times.
so now you know !
c++ example
S32 var = 0;
Con::printf("var is %d", var++);- result is "var is 0".torquescript example
$var = 0;
echo("var is" SPC $var++);- result is "var is 1".About the author
#2
04/12/2006 (2:46 pm)
Silly question... why not fix TorqueScript in this case? I guess there is a possibility of breaking existing script code that counts on this oddity of the language, but those cases should be pretty few.
#3
but yr right, it should be fixed.
i count 347 instances of "++" in our current torquescript codebase,
most of which are for-loops or other trivially disregardable cases,
so there's probably only 50 or so cases which would need individual analysis.
- which isn't very many.
i suspect this might also require getting a deep understanding of the TS compiler,
which isn't something that makes me jump for joy..
04/12/2006 (2:53 pm)
Well it's one of those things which now that i know the issue exists, it won't benefit our project to fix it, and i've got a lot of things on my plate which will, so.but yr right, it should be fixed.
i count 347 instances of "++" in our current torquescript codebase,
most of which are for-loops or other trivially disregardable cases,
so there's probably only 50 or so cases which would need individual analysis.
- which isn't very many.
i suspect this might also require getting a deep understanding of the TS compiler,
which isn't something that makes me jump for joy..
#4
04/12/2006 (2:53 pm)
I agree, this is a bug.
#5
04/12/2006 (2:58 pm)
This has already been discussed in the forums.
#6
04/12/2006 (3:55 pm)
Is this something GG is going to fix?
#7
If it doesn't, technically speaking, I would say the bug here is actually the assumption that its supposed to behave that way.
04/12/2006 (5:02 pm)
Out of curiousity, does it state anywhere that $var++ is supposed to be have like the C/C++ equivalent?If it doesn't, technically speaking, I would say the bug here is actually the assumption that its supposed to behave that way.
#8
which doesn't specify whether it increments before or after the variable is used in the present expression,
so, technically, no, it's not a bug as far as that page's specification is concerned.
however,
of all the languages which have a "++" operator, i'll lay money that torquescript is the only one which implements it this way.
04/12/2006 (5:14 pm)
www.garagegames.com/docs/tge/general/apcs03.php merely saysQuote:
++
Increment. Adds the value of 1 from a variable representing a number.
which doesn't specify whether it increments before or after the variable is used in the present expression,
so, technically, no, it's not a bug as far as that page's specification is concerned.
however,
of all the languages which have a "++" operator, i'll lay money that torquescript is the only one which implements it this way.
#9
Ian
04/12/2006 (6:15 pm)
Nowhere does it state that a + b adds two numbers together, but it's nonetheless expected. I think it's fair to call this a bug, but it's a bug from 2001, and I think that "fixing" it now would break so much code as to make the lesser of two evils leaving it well alone.Ian
Associate Orion Elenzil
Real Life Plus
this doesn't change the functioning of for-loops:
but it DOES change the functioning of things like this:
- in this case, array[0] will be undefined,
and array[1] thru array[4] will be,
which obviously leads to unexpected results when you
then try to use $array in the C++ manner, eg: