// incdec.oad - test increment/decrement operators
// Of note - the following fragment:
//
// x++
//
// is actually treated like this for overloaded ++/--:
//
// var $tmp = x;
// x = $tmp.operator ++();
//
// Therefore, in general, the ++/-- operators should return a new object
// derived from the current object, appropriately incremented or decremented
// This is just a guideline, though. Programs can have side effects if
// they desire. One interesting side effect might be to have a semaphore
// class where ++ locks the semaphore and -- unlocks it. For this kind
// of implementation, the operator should return self
class incdec {
var v;
public proc create(n) {v = n;}
operator ++ () {var vv = v; vv++; return new incdec(vv);}
operator -- () {var vv = v; vv--; return new incdec(vv);}
operator => (typ) {assert typ == String; return v=>String;}
}
proc main()
{
var a = new incdec(3 .iterate());
"a: ", String(a), '\n';
a++;
"a: ", String(a), '\n';
a--;
"a: ", String(a), '\n';
// Showing the equivalency of the shortcut syntax and the call syntax
a = a.operator --(a);
"a: ", String(a), '\n';
}