-
Notifications
You must be signed in to change notification settings - Fork 482
Expand file tree
/
Copy pathStdlib_Nullable.resi
More file actions
287 lines (223 loc) · 6.8 KB
/
Stdlib_Nullable.resi
File metadata and controls
287 lines (223 loc) · 6.8 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
/***
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`.
*/
@unboxed
type t<'a> = Primitive_js_extern.nullable<'a> =
| Value('a)
| @as(null) Null
| @as(undefined) Undefined
/**
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"
/**
The value `undefined`.
See [`undefined`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/undefined) on MDN.
## Examples
```rescript
Console.log(undefined) // Logs `undefined` to the console.
```
*/
external undefined: t<'a> = "#undefined"
/**
`isNullable(a)` returns `true` if `a` is null or undefined, `false` otherwise.
## 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
// Check if asNullable is not null or undefined
switch asNullable->Nullable.isNullable {
| true => assert(false)
| false => assert(true)
}
```
*/
external isNullable: t<'a> => bool = "#is_nullable"
/**
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) => Stdlib_Ordering.t) => Stdlib_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`.
`None` compiles to `undefined` and after running `Nullable.fromOption` on it, it will continue to be `undefined`.
## 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(Nullable.make("Apple"), "Banana") // Apple
let greet = (firstName: option<string>) => "Greetings " ++ firstName->Option.getOr("Anonymous")
Nullable.make("Jane")->Nullable.toOption->greet // "Greetings Jane"
Nullable.null->Nullable.toOption->greet // "Greetings Anonymous"
```
*/
let getOr: (t<'a>, 'a) => 'a
@deprecated({
reason: "Use getOr instead",
migrate: Nullable.getOr(),
})
let getWithDefault: (t<'a>, 'a) => 'a
/**
`getExn(value)` throws an exception if `null` or `undefined`, otherwise returns the value.
```rescript
switch Nullable.getExn(%raw("'Hello'")) {
| exception Invalid_argument(_) => assert(false)
| value => value == "Hello"
}
switch Nullable.getExn(%raw("null")) {
| exception Invalid_argument(_) => assert(true)
| _ => assert(false)
}
switch Nullable.getExn(%raw("undefined")) {
| exception Invalid_argument(_) => assert(true)
| _ => assert(false)
}
```
## Exceptions
- Throws `Invalid_argument` if `value` is `null` or `undefined`
*/
@deprecated({
reason: "Use `getOrThrow` instead",
migrate: Nullable.getOrThrow(),
})
let getExn: t<'a> => 'a
/**
`getOrThrow(value)` throws an exception if `null` or `undefined`, otherwise returns the value.
```rescript
switch Nullable.getOrThrow(%raw("'Hello'")) {
| exception Invalid_argument(_) => assert(false)
| value => value == "Hello"
}
switch Nullable.getOrThrow(%raw("null")) {
| exception Invalid_argument(_) => assert(true)
| _ => assert(false)
}
switch Nullable.getOrThrow(%raw("undefined")) {
| exception Invalid_argument(_) => assert(true)
| _ => assert(false)
}
```
## Exceptions
- Throws `Invalid_argument` if `value` is `null` or `undefined`
*/
let getOrThrow: t<'a> => 'a
/**
`getUnsafe(value)` returns `value`.
## Examples
```rescript
Nullable.getUnsafe(Nullable.make(3)) == 3
Nullable.getUnsafe(Nullable.null) // Throws 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({
reason: "Use mapOr instead",
migrate: Nullable.mapOr(),
})
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>
/**
`ignore(nullable)` ignores the provided nullable and returns unit.
This helper is useful when you want to discard a value (for example, the result of an operation with side effects)
without having to store or process it further.
*/
external ignore: t<'a> => unit = "%ignore"