forked from rescript-lang/rescript-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCore__Nullable.resi
182 lines (140 loc) · 4.29 KB
/
Core__Nullable.resi
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
/***
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"
/**
The value `undefined`.
See [`undefined`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/undefined) on MDN.
## Examples
```rescript
Console.log(Nullable.undefined) // Logs `undefined` to the console.
```
*/
external undefined: t<'a> = "#undefined"
/**
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"
/**
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>
/**
`getWithDefault(value, default)` returns `value` if not `null` or `undefined`,
otherwise return `default`.
## Examples
```rescript
Nullable.getWithDefault(Nullable.null, "Banana") // Banana
Nullable.getWithDefault(Nulalble.make("Apple"), "Banana") // Apple
let greet = (firstName: option<string>) =>
"Greetings " ++ firstName->Nullable.getWithDefault("Anonymous")
Nullable.make("Jane")->greet // "Greetings Jane"
Nullable.null->greet // "Greetings Anonymous"
```
*/
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"
/**
`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(Nullable.undefined, x => x * x) // Nullable.undefined
```
*/
let map: (t<'a>, 'a => 'b) => t<'b>
/**
`mapWithDefault(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.mapWithDefault(0, x => x + 5) // 8
let noneValue = Nullable.null
noneValue->Nullable.mapWithDefault(0, x => x + 5) // 0
```
*/
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) // Nullable.undefined
Nullable.flatMap(Nullable.null, addIfAboveOne) // Nullable.undefined
```
*/
let flatMap: (t<'a>, 'a => t<'b>) => t<'b>