-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecltype.rts
47 lines (41 loc) · 1.16 KB
/
decltype.rts
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
// this:
//
// const a = [1, 2, 3];
//
// is a shorthand for this:
//
// const a: ~[1, 2, 3] = [1, 2, 3];
//
// ~ is a "deliteralization" operator, which converts any literal to their
// generic type (like 1 to number or "foo" to string) and converts tuples to
// typed arrays (like [1, "foo"] to (number | string)[])
const a = [1, 2, 3];
// this:
//
// const b: a = [1, 2, 3];
//
// will make b have the type of the current VALUE of a, which is [1, 2, 3]
//
// we can instead write this:
//
// const b: ~a = [1, 2, 3];
//
// or this:
//
// const b: decltype a = [1, 2, 3];
//
// decltype is an operator that returns "declared type" for a variable
// `~a` and `decltype a` will not always be the same, but in this case they are
//
// const c: [number, boolean] = [1, false];
//
// `decltype c` is equal to [number, boolean]
// `~c` is equal to (number | boolean)[]
const c: [number, boolean] = [1, false];
println(" a = " + to_string(a));
println(" ~a = " + to_string(~a));
println("decltype a = " + to_string(decltype a));
println("");
println(" c = " + to_string(c));
println(" ~c = " + to_string(~c));
println("decltype c = " + to_string(decltype c));