-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathCore__Nullable.resi
More file actions
341 lines (148 loc) · 4.72 KB
/
Core__Nullable.resi
File metadata and controls
341 lines (148 loc) · 4.72 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/***
Functions for handling nullable values.
Primarily useful when interoping with JavaScript when you don't know whether you'll get a value, `null` or `undefined`.
*/
/**
Type representing a nullable value.
A nullable value can be the value `'a`, `null` or `undefined`.
*/
type t<'a> = Js.Nullable.t<'a>
/**
The value `null`.
See [`null`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/null) on MDN.
## Examples
```rescript
Console.log(Nullable.null) // Logs `null` to the console.
```
*/
external null: t<'a> = "#null"
/**
Creates a new nullable value from the provided value.
This means the compiler will enforce null checks for the new value.
## Examples
```rescript
let myStr = "Hello"
let asNullable = myStr->Nullable.make
// Can't do the below because we're now forced to check for nullability
// myStr == asNullable
// Need to do this
switch asNullable->Nullable.toOption {
| Some(value) if value == myStr => Console.log("Yay, values matched!")
| _ => Console.log("Values did not match.")
}
```
*/
external make: 'a => t<'a> = "%identity"
let equal: (t<'a>, t<'b>, ('a, 'b) => bool) => bool
let compare: (t<'a>, t<'b>, ('a, 'b) => Core__Ordering.t) => Core__Ordering.t
/**
Converts a nullable value into an option, so it can be pattern matched on.
Will convert both `null` and `undefined` to `None`, and a present value to `Some(value)`.
## Examples
```rescript
let nullableString = Nullable.make("Hello")
switch nullableString->Nullable.toOption {
| Some(str) => Console.log2("Got string:", str)
| None => Console.log("Didn't have a value.")
}
```
*/
external toOption: t<'a> => option<'a> = "#nullable_to_opt"
/**
Turns an `option` into a `Nullable.t`.
## Examples
```rescript
let optString = Some("Hello")
let asNullable = optString->Nullable.fromOption // Nullable.t<string>
```
*/
let fromOption: option<'a> => t<'a>
/**
`getOr(value, default)` returns `value` if not `null` or `undefined`,
otherwise return `default`.
## Examples
```rescript
Nullable.getOr(Nullable.null, "Banana") // Banana
Nullable.getOr(Nulalble.make("Apple"), "Banana") // Apple
let greet = (firstName: option<string>) =>
"Greetings " ++ firstName->Nullable.getOr("Anonymous")
Nullable.make("Jane")->greet // "Greetings Jane"
Nullable.null->greet // "Greetings Anonymous"
```
*/
let getOr: (t<'a>, 'a) => 'a
@deprecated("Use getOr instead")
let getWithDefault: (t<'a>, 'a) => 'a
/**
`getExn(value)` raises an exception if `null` or `undefined`, otherwise returns the value.
```rescript
Nullable.getExn(Nullable.make(3)) // 3
Nullable.getExn(Nullable.null) /* Raises an Error */
```
## Exceptions
- Raises `Invalid_argument` if `value` is `null` or `undefined`
*/
let getExn: t<'a> => 'a
/**
`getUnsafe(value)` returns `value`.
## Examples
```rescript
Nullable.getUnsafe(Nullable.make(3)) == 3
Nullable.getUnsafe(Nullable.null) // Raises an error
```
## Important
- This is an unsafe operation, it assumes `value` is not `null` or `undefined`.
*/
external getUnsafe: t<'a> => 'a = "%identity"
/**
`forEach(value, f)` call `f` on `value`. if `value` is not `null` or `undefined`,
then if calls `f`, otherwise returns `unit`.
## Examples
```rescript
Nullable.forEach(Nullable.make("thing"), x => Console.log(x)) // logs "thing"
Nullable.forEach(Nullable.null, x => Console.log(x)) // returns ()
Nullable.forEach(undefined, x => Console.log(x)) // returns ()
```
*/
let forEach: (t<'a>, 'a => unit) => unit
/**
`map(value, f)` returns `f(value)` if `value` is not `null` or `undefined`,
otherwise returns `value` unchanged.
## Examples
```rescript
Nullable.map(Nullable.make(3), x => x * x) // Nullable.make(9)
Nullable.map(undefined, x => x * x) // undefined
```
*/
let map: (t<'a>, 'a => 'b) => t<'b>
/**
`mapOr(value, default, f)` returns `f(value)` if `value` is not `null`
or `undefined`, otherwise returns `default`.
## Examples
```rescript
let someValue = Nullable.make(3)
someValue->Nullable.mapOr(0, x => x + 5) // 8
let noneValue = Nullable.null
noneValue->Nullable.mapOr(0, x => x + 5) // 0
```
*/
let mapOr: (t<'a>, 'b, 'a => 'b) => 'b
@deprecated("Use mapOr instead")
let mapWithDefault: (t<'a>, 'b, 'a => 'b) => 'b
/**
`flatMap(value, f)` returns `f(value)` if `value` is not `null` or `undefined`,
otherwise returns `value` unchanged.
## Examples
```rescript
let addIfAboveOne = value =>
if (value > 1) {
Nullable.make(value + 1)
} else {
Nullable.null
}
Nullable.flatMap(Nullable.make(2), addIfAboveOne) // Nullable.make(3)
Nullable.flatMap(Nullable.make(-4), addIfAboveOne) // undefined
Nullable.flatMap(Nullable.null, addIfAboveOne) // undefined
```
*/
let flatMap: (t<'a>, 'a => t<'b>) => t<'b>