From 2c365ada5bfb0fb01ae9e504244c53504ab7f18b Mon Sep 17 00:00:00 2001 From: Max Graey Date: Thu, 21 Feb 2019 01:23:46 +0200 Subject: [PATCH] Improve operator overload typings (#480) --- std/assembly/index.d.ts | 14 +++-- tests/compiler/std/operator-overloading.ts | 60 +++++++++++----------- 2 files changed, 40 insertions(+), 34 deletions(-) diff --git a/std/assembly/index.d.ts b/std/assembly/index.d.ts index d91cc791..7d0927d0 100644 --- a/std/assembly/index.d.ts +++ b/std/assembly/index.d.ts @@ -931,7 +931,10 @@ declare function global( ): TypedPropertyDescriptor | void; /** Annotates a method as a binary operator overload for the specified `token`. */ -declare function operator(token: string): ( +declare function operator(token: + "[]" | "[]=" | "{}" | "{}=" | "==" | "!=" | ">" | "<" | "<=" | ">=" | + ">>" | ">>>" | "<<" | "&" | "|" | "^" | "+" | "-" | "*" | "**" | "/" | "%" +): ( target: any, propertyKey: string, descriptor: TypedPropertyDescriptor @@ -939,19 +942,22 @@ declare function operator(token: string): ( declare namespace operator { /** Annotates a method as a binary operator overload for the specified `token`. */ - export function binary(token: string): ( + export function binary(token: + "[]" | "[]=" | "{}" | "{}=" | "==" | "!=" | ">" | "<" | "<=" | ">=" | + ">>" | ">>>" | "<<" | "&" | "|" | "^" | "+" | "-" | "*" | "**" | "/" | "%" + ): ( target: any, propertyKey: string, descriptor: TypedPropertyDescriptor ) => TypedPropertyDescriptor | void; /** Annotates a method as an unary prefix operator overload for the specified `token`. */ - export function prefix(token: string): ( + export function prefix(token: "!" | "~" | "+" | "-" | "++" | "--"): ( target: any, propertyKey: string, descriptor: TypedPropertyDescriptor ) => TypedPropertyDescriptor | void; /** Annotates a method as an unary postfix operator overload for the specified `token`. */ - export function postfix(token: string): ( + export function postfix(token: "++" | "--"): ( target: any, propertyKey: string, descriptor: TypedPropertyDescriptor diff --git a/tests/compiler/std/operator-overloading.ts b/tests/compiler/std/operator-overloading.ts index 58970822..d2098158 100644 --- a/tests/compiler/std/operator-overloading.ts +++ b/tests/compiler/std/operator-overloading.ts @@ -4,137 +4,137 @@ class Tester { constructor(public x: i32, public y: i32) { } - @operator('+') + @operator("+") static add(a: Tester, b: Tester): Tester { return new Tester(a.x + b.x, a.y + b.y); } - @operator('-') + @operator("-") static sub(a: Tester, b: Tester): Tester { return new Tester(a.x - b.x, a.y - b.y); } - @operator('*') + @operator("*") static mul(a: Tester, b: Tester): Tester { return new Tester(a.x * b.x, a.y * b.y); } - @operator('/') + @operator("/") static div(a: Tester, b: Tester): Tester { return new Tester(a.x / b.x, a.y / b.y); } - @operator('%') + @operator("%") static mod(a: Tester, b: Tester): Tester { return new Tester(a.x % b.x, a.y % b.y); } - @operator('**') + @operator("**") static pow(a: Tester, b: Tester): Tester { return new Tester((a.x ** b.x), (a.y ** b.y)); } - @operator('|') + @operator("|") static or(a: Tester, b: Tester): Tester { return new Tester(a.x | b.x, a.y | b.y); } - @operator('&') + @operator("&") static and(a: Tester, b: Tester): Tester { return new Tester(a.x & b.x, a.y & b.y); } - @operator('^') + @operator("^") static xor(a: Tester, b: Tester): Tester { return new Tester(a.x ^ b.x, a.y ^ b.y); } - @operator('==') + @operator("==") static equals(a: Tester, b: Tester): bool { return a.x == b.x && a.y == b.y; } - @operator('!=') + @operator("!=") static notEquals(a: Tester, b: Tester): bool { return a.x != b.x && a.y != b.y; } - @operator('>') + @operator(">") static greater(a: Tester, b: Tester): bool { return a.x > b.x && a.y > b.y; } - @operator('>=') + @operator(">=") static greaterEquals(a: Tester, b: Tester): bool { return a.x >= b.x && a.y >= b.y; } - @operator('<') + @operator("<") static less(a: Tester, b: Tester): bool { return a.x < b.x && a.y < b.y; } - @operator('<=') + @operator("<=") static lessEquals(a: Tester, b: Tester): bool { return a.x <= b.x && a.y <= b.y; } - @operator('>>') + @operator(">>") static shr(value: Tester, shift: i32): Tester { return new Tester(value.x >> shift, value.y >> shift); } - @operator('>>>') + @operator(">>>") static shu(value: Tester, shift: i32): Tester { return new Tester(value.x >>> shift, value.y >>> shift); } - @operator('<<') + @operator("<<") static shl(value: Tester, shift: i32): Tester { return new Tester(value.x << shift, value.y << shift); } // unary opterators - @operator.prefix('~') + @operator.prefix("~") static not(value: Tester): Tester { return new Tester(~value.x, ~value.y); } - @operator.prefix('!') + @operator.prefix("!") static excl(value: Tester): bool { return !value.x && !value.y; } - @operator.prefix('+') + @operator.prefix("+") static pos(value: Tester): Tester { return new Tester(+value.x, +value.y); } - @operator.prefix('-') + @operator.prefix("-") static neg(value: Tester): Tester { return new Tester(-value.x, -value.y); } - @operator.prefix('++') + @operator.prefix("++") inc(): this { ++this.x; ++this.y; return this; } - @operator.prefix('--') + @operator.prefix("--") dec(): this { --this.x; --this.y; return this; } - @operator.postfix('++') + @operator.postfix("++") postInc(): Tester { return new Tester(this.x + 1, this.y + 1); } - @operator.postfix('--') + @operator.postfix("--") postDec(): Tester { return new Tester(this.x - 1, this.y - 1); } @@ -297,12 +297,12 @@ assert(incdec.x == 0 && incdec.y == 1); class TesterInlineStatic { constructor(public x: i32, public y: i32) { } - @inline @operator('+') + @inline @operator("+") static add(a: TesterInlineStatic, b: TesterInlineStatic): TesterInlineStatic { return new TesterInlineStatic(a.x + b.x, a.y + b.y); } - @inline @operator.postfix('++') + @inline @operator.postfix("++") static postInc(a: TesterInlineStatic): TesterInlineStatic { return new TesterInlineStatic(a.x + 1, a.y + 1); } @@ -317,12 +317,12 @@ assert(ais.x == 4 && ais.y == 6); class TesterInlineInstance { constructor(public x: i32, public y: i32) { } - @inline @operator('+') + @inline @operator("+") add(b: TesterInlineInstance): TesterInlineInstance { return new TesterInlineInstance(this.x + b.x, this.y + b.y); } - @inline @operator.postfix('++') + @inline @operator.postfix("++") postInc(): TesterInlineInstance { return new TesterInlineInstance(this.x + 1, this.y + 1); }