Skip to content

Commit 2c365ad

Browse files
MaxGraeydcodeIO
authored andcommitted
Improve operator overload typings (AssemblyScript#480)
1 parent 951b6f9 commit 2c365ad

File tree

2 files changed

+40
-34
lines changed

2 files changed

+40
-34
lines changed

std/assembly/index.d.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -931,27 +931,33 @@ declare function global(
931931
): TypedPropertyDescriptor<any> | void;
932932

933933
/** Annotates a method as a binary operator overload for the specified `token`. */
934-
declare function operator(token: string): (
934+
declare function operator(token:
935+
"[]" | "[]=" | "{}" | "{}=" | "==" | "!=" | ">" | "<" | "<=" | ">=" |
936+
">>" | ">>>" | "<<" | "&" | "|" | "^" | "+" | "-" | "*" | "**" | "/" | "%"
937+
): (
935938
target: any,
936939
propertyKey: string,
937940
descriptor: TypedPropertyDescriptor<any>
938941
) => TypedPropertyDescriptor<any> | void;
939942

940943
declare namespace operator {
941944
/** Annotates a method as a binary operator overload for the specified `token`. */
942-
export function binary(token: string): (
945+
export function binary(token:
946+
"[]" | "[]=" | "{}" | "{}=" | "==" | "!=" | ">" | "<" | "<=" | ">=" |
947+
">>" | ">>>" | "<<" | "&" | "|" | "^" | "+" | "-" | "*" | "**" | "/" | "%"
948+
): (
943949
target: any,
944950
propertyKey: string,
945951
descriptor: TypedPropertyDescriptor<any>
946952
) => TypedPropertyDescriptor<any> | void;
947953
/** Annotates a method as an unary prefix operator overload for the specified `token`. */
948-
export function prefix(token: string): (
954+
export function prefix(token: "!" | "~" | "+" | "-" | "++" | "--"): (
949955
target: any,
950956
propertyKey: string,
951957
descriptor: TypedPropertyDescriptor<any>
952958
) => TypedPropertyDescriptor<any> | void;
953959
/** Annotates a method as an unary postfix operator overload for the specified `token`. */
954-
export function postfix(token: string): (
960+
export function postfix(token: "++" | "--"): (
955961
target: any,
956962
propertyKey: string,
957963
descriptor: TypedPropertyDescriptor<any>

tests/compiler/std/operator-overloading.ts

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,137 +4,137 @@ class Tester {
44
constructor(public x: i32, public y: i32) {
55
}
66

7-
@operator('+')
7+
@operator("+")
88
static add(a: Tester, b: Tester): Tester {
99
return new Tester(a.x + b.x, a.y + b.y);
1010
}
1111

12-
@operator('-')
12+
@operator("-")
1313
static sub(a: Tester, b: Tester): Tester {
1414
return new Tester(a.x - b.x, a.y - b.y);
1515
}
1616

17-
@operator('*')
17+
@operator("*")
1818
static mul(a: Tester, b: Tester): Tester {
1919
return new Tester(a.x * b.x, a.y * b.y);
2020
}
2121

22-
@operator('/')
22+
@operator("/")
2323
static div(a: Tester, b: Tester): Tester {
2424
return new Tester(a.x / b.x, a.y / b.y);
2525
}
2626

27-
@operator('%')
27+
@operator("%")
2828
static mod(a: Tester, b: Tester): Tester {
2929
return new Tester(a.x % b.x, a.y % b.y);
3030
}
3131

32-
@operator('**')
32+
@operator("**")
3333
static pow(a: Tester, b: Tester): Tester {
3434
return new Tester(<i32>(a.x ** b.x), <i32>(a.y ** b.y));
3535
}
3636

37-
@operator('|')
37+
@operator("|")
3838
static or(a: Tester, b: Tester): Tester {
3939
return new Tester(a.x | b.x, a.y | b.y);
4040
}
4141

42-
@operator('&')
42+
@operator("&")
4343
static and(a: Tester, b: Tester): Tester {
4444
return new Tester(a.x & b.x, a.y & b.y);
4545
}
4646

47-
@operator('^')
47+
@operator("^")
4848
static xor(a: Tester, b: Tester): Tester {
4949
return new Tester(a.x ^ b.x, a.y ^ b.y);
5050
}
5151

52-
@operator('==')
52+
@operator("==")
5353
static equals(a: Tester, b: Tester): bool {
5454
return a.x == b.x && a.y == b.y;
5555
}
5656

57-
@operator('!=')
57+
@operator("!=")
5858
static notEquals(a: Tester, b: Tester): bool {
5959
return a.x != b.x && a.y != b.y;
6060
}
6161

62-
@operator('>')
62+
@operator(">")
6363
static greater(a: Tester, b: Tester): bool {
6464
return a.x > b.x && a.y > b.y;
6565
}
6666

67-
@operator('>=')
67+
@operator(">=")
6868
static greaterEquals(a: Tester, b: Tester): bool {
6969
return a.x >= b.x && a.y >= b.y;
7070
}
7171

72-
@operator('<')
72+
@operator("<")
7373
static less(a: Tester, b: Tester): bool {
7474
return a.x < b.x && a.y < b.y;
7575
}
7676

77-
@operator('<=')
77+
@operator("<=")
7878
static lessEquals(a: Tester, b: Tester): bool {
7979
return a.x <= b.x && a.y <= b.y;
8080
}
8181

82-
@operator('>>')
82+
@operator(">>")
8383
static shr(value: Tester, shift: i32): Tester {
8484
return new Tester(value.x >> shift, value.y >> shift);
8585
}
8686

87-
@operator('>>>')
87+
@operator(">>>")
8888
static shu(value: Tester, shift: i32): Tester {
8989
return new Tester(value.x >>> shift, value.y >>> shift);
9090
}
9191

92-
@operator('<<')
92+
@operator("<<")
9393
static shl(value: Tester, shift: i32): Tester {
9494
return new Tester(value.x << shift, value.y << shift);
9595
}
9696

9797
// unary opterators
98-
@operator.prefix('~')
98+
@operator.prefix("~")
9999
static not(value: Tester): Tester {
100100
return new Tester(~value.x, ~value.y);
101101
}
102102

103-
@operator.prefix('!')
103+
@operator.prefix("!")
104104
static excl(value: Tester): bool {
105105
return !value.x && !value.y;
106106
}
107107

108-
@operator.prefix('+')
108+
@operator.prefix("+")
109109
static pos(value: Tester): Tester {
110110
return new Tester(+value.x, +value.y);
111111
}
112112

113-
@operator.prefix('-')
113+
@operator.prefix("-")
114114
static neg(value: Tester): Tester {
115115
return new Tester(-value.x, -value.y);
116116
}
117117

118-
@operator.prefix('++')
118+
@operator.prefix("++")
119119
inc(): this {
120120
++this.x;
121121
++this.y;
122122
return this;
123123
}
124124

125-
@operator.prefix('--')
125+
@operator.prefix("--")
126126
dec(): this {
127127
--this.x;
128128
--this.y;
129129
return this;
130130
}
131131

132-
@operator.postfix('++')
132+
@operator.postfix("++")
133133
postInc(): Tester {
134134
return new Tester(this.x + 1, this.y + 1);
135135
}
136136

137-
@operator.postfix('--')
137+
@operator.postfix("--")
138138
postDec(): Tester {
139139
return new Tester(this.x - 1, this.y - 1);
140140
}
@@ -297,12 +297,12 @@ assert(incdec.x == 0 && incdec.y == 1);
297297
class TesterInlineStatic {
298298
constructor(public x: i32, public y: i32) {
299299
}
300-
@inline @operator('+')
300+
@inline @operator("+")
301301
static add(a: TesterInlineStatic, b: TesterInlineStatic): TesterInlineStatic {
302302
return new TesterInlineStatic(a.x + b.x, a.y + b.y);
303303
}
304304

305-
@inline @operator.postfix('++')
305+
@inline @operator.postfix("++")
306306
static postInc(a: TesterInlineStatic): TesterInlineStatic {
307307
return new TesterInlineStatic(a.x + 1, a.y + 1);
308308
}
@@ -317,12 +317,12 @@ assert(ais.x == 4 && ais.y == 6);
317317
class TesterInlineInstance {
318318
constructor(public x: i32, public y: i32) {
319319
}
320-
@inline @operator('+')
320+
@inline @operator("+")
321321
add(b: TesterInlineInstance): TesterInlineInstance {
322322
return new TesterInlineInstance(this.x + b.x, this.y + b.y);
323323
}
324324

325-
@inline @operator.postfix('++')
325+
@inline @operator.postfix("++")
326326
postInc(): TesterInlineInstance {
327327
return new TesterInlineInstance(this.x + 1, this.y + 1);
328328
}

0 commit comments

Comments
 (0)