-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdictionary.rts
48 lines (35 loc) · 1.44 KB
/
dictionary.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
48
// This function accepts an array of keys and a child type/value. It will create
// an object with all properties from the array assigned to the child type. Note
// that this function can create both objects and object types just as well
// (because they are literally the same thing).
const buildDictionary = fn (keys: string[], childType: any) any {
var i = 0;
const ret: any = {};
// # is an operator that return the array length
while (i < #keys) {
ret[keys[i]] = childType;
i += 1;
}
return ret;
};
const a = buildDictionary(["foo", "bar"], number);
const b = buildDictionary(["baz"], any);
println("a = " + to_string(a));
println("b = " + to_string(b));
// We created two values, `a` and `b`, which we can then use as types (or not).
const x: a = { foo: 1, bar: 2 };
const y: b = { baz: number };
// `x` and `y` are now constrained to a type that was created at runtime.
println("x = " + to_string(x));
println("y = " + to_string(y));
b["baz"] = 1 | 2;
println("b = " + to_string(b));
b["baz"] = buildDictionary(["qux"], 7);
println("b = " + to_string(b));
// If we mutate a variable in a way that it is no longer assignable to its
// declared type, we get a runtime error. `a["foo"]` is supposed to hold
// anything assignable to `number`. We can confirm that by getting the declared
// type of `a` using `decltype` operator and checking its "foo" property.
println((decltype a)["foo"]);
println("Next line should fail");
a["foo"] = "quux";