Add unary postfix operator overloading (#309)

This commit is contained in:
Max Graey
2018-10-30 16:23:18 +02:00
committed by Daniel Wirtz
parent d864bef1eb
commit dc4e2060ba
4 changed files with 595 additions and 96 deletions

View File

@ -128,6 +128,16 @@ class Tester {
--this.y;
return this;
}
@operator.postfix('++')
postInc(): Tester {
return new Tester(this.x + 1, this.y + 1);
}
@operator.postfix('--')
postDec(): Tester {
return new Tester(this.x - 1, this.y - 1);
}
}
// check additional
@ -273,6 +283,16 @@ assert(incdec.x == 1 && incdec.y == 2);
--incdec;
assert(incdec.x == 0 && incdec.y == 1);
incdec = new Tester(0, 1);
var tmp = incdec++;
assert(tmp.x == 0 && tmp.y == 1);
assert(incdec.x == 1 && incdec.y == 2);
tmp = incdec--;
assert(tmp.x == 1 && tmp.y == 2);
assert(incdec.x == 0 && incdec.y == 1);
// check inlined static
class TesterInlineStatic {
constructor(public x: i32, public y: i32) {
@ -281,11 +301,17 @@ class TesterInlineStatic {
static add(a: TesterInlineStatic, b: TesterInlineStatic): TesterInlineStatic {
return new TesterInlineStatic(a.x + b.x, a.y + b.y);
}
@inline @operator.postfix('++')
static postInc(a: TesterInlineStatic): TesterInlineStatic {
return new TesterInlineStatic(a.x + 1, a.y + 1);
}
}
var ais1 = new TesterInlineStatic(1, 2);
ais1++; // 2, 3
var ais2 = new TesterInlineStatic(2, 3);
var ais = ais1 + ais2;
assert(ais.x == 3 && ais.y == 5);
assert(ais.x == 4 && ais.y == 6);
// check inlined instance
class TesterInlineInstance {
@ -295,8 +321,14 @@ class TesterInlineInstance {
add(b: TesterInlineInstance): TesterInlineInstance {
return new TesterInlineInstance(this.x + b.x, this.y + b.y);
}
@inline @operator.postfix('++')
postInc(): TesterInlineInstance {
return new TesterInlineInstance(this.x + 1, this.y + 1);
}
}
var aii1 = new TesterInlineInstance(1, 2);
aii1++; // 2, 3
var aii2 = new TesterInlineInstance(2, 3);
var aii = aii1 + aii2;
assert(aii.x == 3 && aii.y == 5);
assert(aii.x == 4 && aii.y == 6);