-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathCore__Null.resi
More file actions
190 lines (146 loc) · 4.06 KB
/
Core__Null.resi
File metadata and controls
190 lines (146 loc) · 4.06 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
/***
Functions for handling values that could be `null`.
If you also need to cover `undefined`, check out `Nullable` instead.
*/
/**
A type representing a value that can be either `'a` or `null`.
*/
type t<'a> = Js.Null.t<'a>
/**
Converts a `Null.t` into a `Nullable.t`.
## Examples
```rescript
let nullValue = Null.make("Hello")
let asNullable = nullValue->Null.asNullable // Nullable.t<string>
```
*/
external asNullable: t<'a> => Core__Nullable.t<'a> = "%identity"
/**
The value `null`.
See [`null`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/null) on MDN.
## Examples
```rescript
Console.log(null) // Logs `null` to the console.
```
*/
external null: t<'a> = "#null"
/**
Creates a new `Null.t` from the provided value.
This means the compiler will enforce null checks for the new value.
## Examples
```rescript
let myStr = "Hello"
let asNullValue = myStr->Null.make // The compiler now thinks this can be `string` or `null`.
```
*/
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 `null` to `None`, and a present value to `Some(value)`.
## Examples
```rescript
let nullStr = Null.make("Hello")
switch nullStr->Null.toOption {
| Some(str) => Console.log2("Got string:", str)
| None => Console.log("Didn't have a value.")
}
```
*/
external toOption: t<'a> => option<'a> = "#null_to_opt"
/**
Turns an `option` into a `Null.t`. `None` will be converted to `null`.
## Examples
```rescript
let optString: option<string> = None
let asNull = optString->Null.fromOption // Null.t<string>
Console.log(asNull == null) // Logs `true` to the console.
```
*/
let fromOption: option<'a> => t<'a>
/**
`getWithDefault(value, default)` returns `value` if not `null`, otherwise return
`default`.
## Examples
```rescript
Null.getWithDefault(null, "Banana") // Banana
Null.getWithDefault(Nulalble.make("Apple"), "Banana") // Apple
let greet = (firstName: option<string>) =>
"Greetings " ++ firstName->Null.getWithDefault("Anonymous")
Null.make("Jane")->greet // "Greetings Jane"
null->greet // "Greetings Anonymous"
```
*/
let getWithDefault: (t<'a>, 'a) => 'a
/**
`getExn(value)` raises an exception if `null`, otherwise returns the value.
```rescript
Null.getExn(Null.make(3)) // 3
Null.getExn(null) /* Raises an Error */
```
## Exceptions
- Raises `Invalid_argument` if `value` is `null`,
*/
let getExn: t<'a> => 'a
/**
`getUnsafe(value)` returns `value`.
## Examples
```rescript
Null.getUnsafe(Null.make(3)) == 3
Null.getUnsafe(null) // Raises an error
```
## Important
- This is an unsafe operation, it assumes `value` is not `null`.
*/
external getUnsafe: t<'a> => 'a = "%identity"
/**
`map(value, f)` returns `f(value)` if `value` is not `null`, otherwise returns
`value` unchanged.
## Examples
```rescript
Null.map(Null.make(3), x => x * x) // Null.make(9)
Null.map(null, x => x * x) // null
```
*/
let map: (t<'a>, 'a => 'b) => t<'b>
/**
`mapWithDefault(value, default, f)` returns `f(value)` if `value` is not `null`,
otherwise returns `default`.
## Examples
```rescript
let someValue = Null.make(3)
someValue->Null.mapWithDefault(0, x => x + 5) // 8
let noneValue = null
noneValue->Null.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`, otherwise
returns `value` unchanged.
## Examples
```rescript
let addIfAboveOne = value =>
if (value > 1) {
Null.make(value + 1)
} else {
null
}
Null.flatMap(Null.make(2), addIfAboveOne) // Null.make(3)
Null.flatMap(Null.make(-4), addIfAboveOne) // null
Null.flatMap(null, addIfAboveOne) // null
```
*/
let flatMap: (t<'a>, 'a => t<'b>) => t<'b>
/**
`isNull(value)` checks if `value===null`
## Examples
```rescript
"abc"->Null.make->Null.isNull // false
Nullable.undefined->Null.make->Null.isNull // false
Null.null->Null.isNull // true
None->Null.make->Null.isNull // false
```
*/
let isNull: t<'a> => bool