-
Notifications
You must be signed in to change notification settings - Fork 465
/
Copy pathUntaggedVariants.res
198 lines (170 loc) · 3.7 KB
/
UntaggedVariants.res
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
@unboxed
type t = A | I(int) | S(string)
@unboxed
type t2 = S2(string) | I2(float)
let i = I(42)
let i2 = I2(42.5)
let s = S("abc")
let s2 = S2("abc")
let classify = x =>
switch x {
| I(_) => "An integer"
| S(s) => "A string" ++ s
| A => "A"
}
let classify2 = x =>
switch x {
| I2(_) => "A float"
| S2(s) => "A string" ++ s
}
@unboxed
type tt = One | Two | Object({x: int, y: string})
let w = Object({x: 10, y: ""})
let cls = x =>
switch x {
| One => "one"
| Two => "two"
| Object({y}) => "object" ++ y
}
module ListWithTuples = {
@unboxed
type rec t<'a> = | @as(undefined) Empty | Cons(('a, t<'a>))
}
module ListWithObjects = {
@unboxed
type rec t<'a> = | @as(null) Empty | Cons({hd: 'a, tl: t<'a>})
}
let rec tuplesToObjects = (l: ListWithTuples.t<_>): ListWithObjects.t<_> =>
switch l {
| Empty => Empty
| Cons((hd, tl)) => Cons({hd, tl: tuplesToObjects(tl)})
}
let l1 = ListWithTuples.Cons((1, Cons((2, Cons((3, Empty))))))
let l2 = tuplesToObjects(l1)
Js.log2("l1", l1)
Js.log2("l2", l2)
module Truthy = {
@unboxed
type t = | @as(true) True | Obj({flag: bool})
let isTrue = x =>
switch x {
| True => true
| Obj({flag}) => flag
}
}
module TwoObjects = {
@unboxed
type t = | @as(null) Null | Object({name: string}) | @as(undefined) Undefined
let classify = x =>
switch x {
| Null => "null"
| Object({name}) => "object" ++ name
| Undefined => "undefined"
}
}
module Unknown = {
@unboxed
type t<'a> = A | B | Unknown('a)
let classify = x =>
switch x {
| A => "a"
| B => "b"
| Unknown(v) => {
Js.log(x)
"Unknown"
}
}
}
module MultipleBlocks = {
@unboxed
type t<'a> = A | B | C | D | String(string) | Int(int) | Object({name: string})
let classify = x =>
switch x {
| A => "a"
| B => "b"
| C => "c"
| D => "d"
| String(_) => "string"
| Int(_) => "int"
| Object({name}) => "Object" ++ name
}
}
module OnlyBlocks = {
@unboxed
type t<'a> = String(string) | Int(int) | Object({name: string})
let classify = x =>
switch x {
| String(_) => "string"
| Int(_) => "int"
| Object({name}) => "Object" ++ name
}
}
module WithArray = {
@unboxed
type t<'a> = String(string) | Float(float) | Array(array<string>) | Object({name: string})
let classify = x =>
switch x {
| String(_) => "string"
| Float(_) => "int"
| Array(_) if true => "array"
| Array(_) => "array"
| Object({name}) => "Object" ++ name
}
}
module Json = {
@unboxed
type rec t =
| @as(false) False
| @as(true) True
| @as(null) Null
| String(string)
| Number(float)
| Object(Js.Dict.t<t>)
| Array(array<t>)
type tagged_t =
| JSONFalse
| JSONTrue
| JSONNull
| JSONString(string)
| JSONNumber(float)
| JSONObject(Js.Dict.t<t>)
| JSONArray(array<t>)
let classify = (x: t) =>
switch x {
| False => JSONFalse
| True => JSONTrue
| Null => JSONNull
| String(s) => JSONString(s)
| Number(n) => JSONNumber(n)
| Object(o) => JSONObject(o)
| Array(a) => JSONArray(a)
}
/* from js_json.ml
let classify (x : t) : tagged_t =
let ty = Js.typeof x in
if ty = "string" then
JSONString (Obj.magic x)
else if ty = "number" then
JSONNumber (Obj.magic x )
else if ty = "boolean" then
if (Obj.magic x) = true then JSONTrue
else JSONFalse
else if (Obj.magic x) == Js.null then
JSONNull
else if Js_array2.isArray x then
JSONArray (Obj.magic x)
else
JSONObject (Obj.magic x)
*/
}
module TrickyNested = {
@unboxed
type rec t =
| A((t, t))
| B
let check = (s, y) =>
switch s {
| A((A(x), B)) if x !== y => 41
| _ => 42
}
}