++ and --

Veracity

Developer
Staff member
Revision 17454 adds experimental support for X++, X--, ++X, and ==X.

X can be a variable reference or an aggregate reference - a map entry or a record field.

They have the expected meaning:

X++ - increment X and return the old value
++X - increment X and return the new value
X-- - decrement X and return the old value
--X - decrement X and return the new value

Simple test program:

Code:
int a = 1;
int b = a++;

print( "a = " + a );
print( "b = " + b );

print( "a = " + a + " a++ = " + a++ + " a = " + a );
print( "a = " + a + " a-- = " + a-- + " a = " + a );
print( "a = " + a + " ++a = " + ++a + " a = " + a );
print( "a = " + a + " --a = " + --a + " a = " + a );

int [string] map;

map["a"] = 1;
map["b"] = 10;
map["c"] = 100;
map["d"] = 1000;

foreach key, val in map {
    print( "++map[" + key + "] = " + ++map[key] );
}

record {
    int a;
    int b;
    int c;
} rec;

rec.a = 1;
rec.b = rec.a++;
rec.c = ++rec.a;

print( "rec.a = " + rec.a );
print( "rec.b = " + rec.b );
print( "rec.c = " + rec.c );
results in:

Code:
[color=green]> test-incdec[/color]

a = 2
b = 1
a = 2 a++ = 2 a = 3
a = 3 a-- = 3 a = 2
a = 2 ++a = 3 a = 3
a = 3 --a = 2 a = 2
++map[a] = 2
++map[b] = 11
++map[c] = 101
++map[d] = 1001
rec.a = 3
rec.b = 1
rec.c = 3
Tell me if there are any surprises.
 
Last edited:

Bale

Minion
O. Wow.

I remember that a long time ago these were requested and refused by you. Eventually you gave in enough to code += -= *= and /=. What changed? Just bored and wanted to add something?
 
Last edited:

Veracity

Developer
Staff member
I did += -= etc. a long time ago:

Code:
------------------------------------------------------------------------
r8401 | veracity0 | 2010-04-24 14:35:23 -0400 (Sat, 24 Apr 2010) | 3 lines

Provide assignments with operator to ASH: += -= *= /= %= work with interger
or float variables or composite references; += also works with strings.
Those were easier; they are essentially variants on a regular assignment, whereas ++ and -- are types of "values" that can be embedded in expressions.

I had comments in the "operator priority" method that said "here is where prefix ++ and -- would go" and "here is where prefix ++ and == would go".

In any case, I looked at it today and it didn't end up being all that hard.
 

Bale

Minion
Glad to know. I just brought up -+ etc because they evolved out of the same conversation where ++ and -- were discussed.
 
Top