- Notifications
You must be signed in to change notification settings - Fork 670
/
Copy pathcall-rest.ts
54 lines (48 loc) · 1.17 KB
/
call-rest.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
// direct call test
functionfn(a: i32,b: i32=0, ...rest: i32[]): i32{
letsum=a+b;
for(leti=0,k=rest.length;i<k;++i){
sum+=rest[i];
}
returnsum;
}
assert(fn(1)==1);
assert(fn(1,2)==3);
assert(fn(1,2,3)==6);
assert(fn(1,2,3,4,5)==15);
// indirect call test
varindirect=fn;
assert(indirect(1)==1);
assert(indirect(1,2)==3);
assert(indirect(1,2,3)==6);
assert(indirect(1,2,3,4,5)==15);
// constructor test
classFoo{
values: i32[];
constructor(a: i32,b: i32=0, ...rest: i32[]){
this.values=[a,b];
for(leti=0,k=rest.length;i<k;++i){
this.values.push(rest[i]);
}
}
sum(): i32{
letsum=0;
for(leti=0,k=this.values.length;i<k;++i){
sum+=this.values[i];
}
returnsum;
}
}
assert(newFoo(1).sum()==1);
assert(newFoo(1,2).sum()==3);
assert(newFoo(1,2,3).sum()==6);
assert(newFoo(1,2,3,4,5).sum()==15);
// generic test
functioncount<T>(...args: T[]): i32{
returnargs.length;
}
assert(count<i32>()==0);
assert(count<i32>(1)==1);
assert(count<i32>(1,2,3)==3);
// inferred generic test
assert(count('a','b','c')==3);