File tree 1 file changed +72
-0
lines changed
1 file changed +72
-0
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ id : " set-index-decorator"
3
+ keywords : ["set", "decorator", "array", "object"]
4
+ name : " @set_index"
5
+ summary : " This is the `@set_index` decorator."
6
+ category : " decorators"
7
+ ---
8
+
9
+ The ` @set_index ` decorator is used to set a dynamic property or index.
10
+
11
+ For example with an Array:
12
+
13
+ <CodeTab labels = { [" ReScript" , " JS Output" ]} >
14
+
15
+ ``` res
16
+ type t
17
+
18
+ @new external create: int => t = "Array"
19
+ @set_index external set: (t, int, string) => unit = ""
20
+ @get_index external get: (t, int) => string = ""
21
+
22
+ let a = create(3)
23
+ a->set(0, "zero")
24
+ a->set(1, "one")
25
+ a->set(2, "two")
26
+
27
+ let value = a->get(1)
28
+ ```
29
+
30
+ ``` js
31
+ var a = new Array (3 );
32
+
33
+ a[0 ] = " zero" ;
34
+ a[1 ] = " one" ;
35
+ a[2 ] = " two" ;
36
+
37
+ var value = a[1 ];
38
+ ```
39
+
40
+ </CodeTab >
41
+
42
+ And an example with an object:
43
+
44
+ <CodeTab labels = { [" ReScript" , " JS Output" ]} >
45
+
46
+ ``` res
47
+ type t
48
+
49
+ @new external create: unit => t = "Object"
50
+ @set_index external set: (t, string, int) => unit = ""
51
+ @get_index external get: (t, string) => int = ""
52
+
53
+ let o = create()
54
+ o->set("x", 1)
55
+ o->set("y", 3)
56
+ o->set("z", 5)
57
+
58
+ let value = o->get("y")
59
+ ```
60
+
61
+ ``` js
62
+ var o = new Object ();
63
+
64
+ o[" x" ] = 1 ;
65
+ o[" y" ] = 3 ;
66
+ o[" z" ] = 5 ;
67
+
68
+ var value = o[" y" ];
69
+ ```
70
+
71
+ </CodeTab >
72
+
You can’t perform that action at this time.
0 commit comments