mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-16 08:21:44 +00:00
Add unary postfix operator overloading (#309)
This commit is contained in:
@ -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);
|
||||
|
Reference in New Issue
Block a user