forked from AssemblyScript/assemblyscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-overloading.ts
209 lines (191 loc) · 3.2 KB
/
class-overloading.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
var which: string = "";
class A {
a<T>(a: T): void { // virtual
which = "A";
}
b(b: i32): void { // virtual
which = "A";
}
get c(): i32 { // virtual
which = "A";
return 0;
}
set c(c: i32) { // virtual
which = "A";
}
}
class B extends A {
a<T>(a: T): void { // virtual + overload
which = "B";
}
b(b: i32): void { // virtual + overload
which = "B";
}
get c(): i32 { // virtual + overload
which = "B";
return 0;
}
set c(c: i32) { // virtual + overload
which = "B";
}
}
// Should call the overload
var a: A = new B();
a.a<i32>(1);
assert(which == "B");
which = "";
a.b(1);
assert(which == "B");
which = "";
a.c;
assert(which == "B");
which = "";
a.c = 1;
assert(which == "B");
class C extends B {
a<T>(a: T): void { // overload
super.a(a);
assert(which == "B");
which = "C";
}
b(b: i32): void { // overload
which = "C";
}
get c(): i32 { // overload
which = "C";
return 0;
}
set c(c: i32) { // overload
which = "C";
}
}
// Should call non-virtual super
var c = new C();
which = "";
c.a<i32>(1);
assert(which == "C");
which = "";
c.b(1);
assert(which == "C");
which = "";
c.c;
assert(which == "C");
c.c = 1;
assert(which == "C");
class D extends B {
// inherits B's
}
// Should call inherited overload
a = new D();
which = "";
a.a<i32>(1);
assert(which == "B");
which = "";
a.b(1);
assert(which == "B");
which = "";
a.c;
assert(which == "B");
a.c = 1;
assert(which == "B");
class E extends D {
// inherits B's
}
// Should still call inherited overload
a = new E();
which = "";
a.a<i32>(1);
assert(which == "B");
which = "";
a.b(1);
assert(which == "B");
which = "";
a.c;
assert(which == "B");
a.c = 1;
assert(which == "B");
class F extends E {
a<T>(a: T): void { // overload
which = "F";
}
b(b: i32): void { // overload
which = "F";
}
get c(): i32 { // overload
which = "F";
return 0;
}
set c(c: i32) { // overload
which = "F";
}
}
// Should no longer call inherited overload
a = new F();
which = "";
a.a<i32>(1);
assert(which == "F");
which = "";
a.b(1);
assert(which == "F");
which = "";
a.c;
assert(which == "F");
which = "";
a.c = 1;
assert(which == "F");
// Should work with interfaces
interface IA {
foo(): void;
}
class CA implements IA {
foo(): void {
which = "IB";
}
}
var ia: IA = new CA();
which = "";
ia.foo();
assert(which == "IB");
// Should work with extended interfaces
interface IC extends IA {
}
class CC implements IC {
foo(): void {
which = "IC";
}
}
var ic: IC = new CC();
which = "";
ic.foo();
assert(which == "IC");
// Should make stubs for functions discovered when compiling other virtual stubs
class A1 {
public bar(): i32 {
return this.baz();
// 4) discovers A1#baz
}
public baz(): i32 {
throw new Error("not implemented");
// 5) discovers B1#baz (overload)
}
}
class B1 extends A1 {
public baz(): i32 {
return 3;
// 6) complete
}
}
class A2 {
foo(): i32 {
throw new Error("not implemented");
// 2) discovers B2#foo (overload)
}
}
class B2 extends A2 {
foo(): i32 {
return new B1().bar();
// 3) discovers B1#bar (alias of A1#bar)
}
}
var b2: A2 = new B2();
assert(b2.foo() == 3); // 1) discovers A2#foo