Skip to content

Commit 634c11a

Browse files
authored
docs for Null, Nullable and Undefined (#45)
1 parent 4f2e82b commit 634c11a

File tree

3 files changed

+231
-0
lines changed

3 files changed

+231
-0
lines changed

src/Core__Null.resi

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/***
2+
Functions for handling values that could be `null`.
3+
4+
If you also need to cover `undefined`, check out `Nullable` instead.
5+
*/
6+
7+
/**
8+
A type representing a value that can be either `'a` or `null`.
9+
*/
10+
type t<'a> = Js.Null.t<'a>
11+
12+
/**
13+
Converts a `Null.t` into a `Nullable.t`.
14+
15+
## Examples
16+
```rescript
17+
let nullValue = Null.make("Hello")
18+
let asNullable = nullValue->Null.asNullable // Nullable.t<string>
19+
```
20+
*/
21+
external asNullable: t<'a> => Core__Nullable.t<'a> = "%identity"
22+
23+
/**
24+
The value `null`.
25+
26+
See [`null`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/null) on MDN.
27+
28+
## Examples
29+
```rescript
30+
Console.log(Null.null) // Logs `null` to the console.
31+
```
32+
*/
33+
external null: t<'a> = "#null"
34+
35+
/**
36+
Creates a new `Null.t` from the provided value.
37+
This means the compiler will enforce null checks for the new value.
38+
39+
## Examples
40+
```rescript
41+
let myStr = "Hello"
42+
let asNullValue = myStr->Null.make // The compiler now thinks this can be `string` or `null`.
43+
```
44+
*/
45+
external make: 'a => t<'a> = "%identity"
46+
47+
/**
48+
Converts a nullable value into an option, so it can be pattern matched on.
49+
Will convert `null` to `None`, and a present value to `Some(value)`.
50+
51+
## Examples
52+
```rescript
53+
let nullStr = Null.make("Hello")
54+
55+
switch nullStr->Null.toOption {
56+
| Some(str) => Console.log2("Got string:", str)
57+
| None => Console.log("Didn't have a value.")
58+
}
59+
```
60+
*/
61+
external toOption: t<'a> => option<'a> = "#null_to_opt"
62+
63+
/**
64+
Turns an `option` into a `Null.t`. `None` will be converted to `null`.
65+
66+
## Examples
67+
```rescript
68+
let optString: option<string> = None
69+
let asNull = optString->Null.fromOption // Null.t<string>
70+
Console.log(asNull == Null.null) // Logs `true` to the console.
71+
```
72+
*/
73+
let fromOption: option<'a> => t<'a>

src/Core__Nullable.resi

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/***
2+
Functions for handling nullable values.
3+
4+
Primarily useful when interoping with JavaScript when you don't know whether you'll get a value, `null` or `undefined`.
5+
*/
6+
7+
/**
8+
Type representing a nullable value.
9+
A nullable value can be the value `'a`, `null` or `undefined`.
10+
*/
11+
type t<'a> = Js.Nullable.t<'a>
12+
13+
/**
14+
The value `null`.
15+
16+
See [`null`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/null) on MDN.
17+
18+
## Examples
19+
```rescript
20+
Console.log(Nullable.null) // Logs `null` to the console.
21+
```
22+
*/
23+
external null: t<'a> = "#null"
24+
25+
/**
26+
The value `undefined`.
27+
28+
See [`undefined`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/undefined) on MDN.
29+
30+
## Examples
31+
```rescript
32+
Console.log(Nullable.undefined) // Logs `undefined` to the console.
33+
```
34+
*/
35+
external undefined: t<'a> = "#undefined"
36+
37+
/**
38+
Creates a new nullable value from the provided value.
39+
This means the compiler will enforce null checks for the new value.
40+
41+
## Examples
42+
```rescript
43+
let myStr = "Hello"
44+
let asNullable = myStr->Nullable.make
45+
46+
// Can't do the below because we're now forced to check for nullability
47+
// myStr == asNullable
48+
49+
// Need to do this
50+
switch asNullable->Nullable.toOption {
51+
| Some(value) if value == myStr => Console.log("Yay, values matched!")
52+
| _ => Console.log("Values did not match.")
53+
}
54+
```
55+
*/
56+
external make: 'a => t<'a> = "%identity"
57+
58+
/**
59+
Converts a nullable value into an option, so it can be pattern matched on.
60+
Will convert both `null` and `undefined` to `None`, and a present value to `Some(value)`.
61+
62+
## Examples
63+
```rescript
64+
let nullableString = Nullable.make("Hello")
65+
66+
switch nullableString->Nullable.toOption {
67+
| Some(str) => Console.log2("Got string:", str)
68+
| None => Console.log("Didn't have a value.")
69+
}
70+
```
71+
*/
72+
external toOption: t<'a> => option<'a> = "#nullable_to_opt"
73+
74+
/**
75+
Turns an `option` into a `Nullable.t`.
76+
77+
## Examples
78+
```rescript
79+
let optString = Some("Hello")
80+
let asNullable = optString->Nullable.fromOption // Nullable.t<string>
81+
```
82+
*/
83+
let fromOption: option<'a> => t<'a>

src/Core__Undefined.resi

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/***
2+
Functions for handling values that could be `undefined`.
3+
4+
Please note that a regular `option` is also represented as `undefined` or the value `'a`. In many cases it makes sense to use `option` directly.
5+
6+
If you also need to cover `null`, check out `Nullable` instead.
7+
*/
8+
9+
/**
10+
A type representing a value that can be either `'a` or `undefined`.
11+
*/
12+
type t<'a> = Js.Undefined.t<'a>
13+
14+
/**
15+
Converts a `Undefined.t` into a `Nullable.t`.
16+
17+
## Examples
18+
```rescript
19+
let undefinedValue = Undefined.make("Hello")
20+
let asNullable = undefinedValue->Undefined.asNullable // Nullable.t<string>
21+
```
22+
*/
23+
external asNullable: t<'a> => Core__Nullable.t<'a> = "%identity"
24+
25+
/**
26+
The value `undefined`.
27+
28+
See [`undefined`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/undefined) on MDN.
29+
30+
## Examples
31+
```rescript
32+
Console.log(Undefined.undefined) // Logs `undefined` to the console.
33+
```
34+
*/
35+
external undefined: t<'a> = "#undefined"
36+
37+
/**
38+
Creates a new `Undefined.t` from the provided value.
39+
This means the compiler will enforce `undefined` checks for the new value.
40+
41+
## Examples
42+
```rescript
43+
let myStr = "Hello"
44+
let asUndefinedValue = myStr->Undefined.make // The compiler now thinks this can be `string` or `undefined`.
45+
```
46+
*/
47+
external make: 'a => t<'a> = "%identity"
48+
49+
/**
50+
Converts a potentially undefined value into an option, so it can be pattern matched on.
51+
Will convert `undefined` to `None`, and a present value to `Some(value)`.
52+
53+
## Examples
54+
```rescript
55+
let undefinedStr = Undefined.make("Hello")
56+
57+
switch undefinedStr->Undefined.toOption {
58+
| Some(str) => Console.log2("Got string:", str)
59+
| None => Console.log("Didn't have a value.")
60+
}
61+
```
62+
*/
63+
external toOption: t<'a> => option<'a> = "#undefined_to_opt"
64+
65+
/**
66+
Turns an `option` into a `Undefined.t`. `None` will be converted to `undefined`.
67+
68+
## Examples
69+
```rescript
70+
let optString: option<string> = None
71+
let asUndefined = optString->Undefined.fromOption // Undefined.t<string>
72+
Console.log(asUndefined == Undefined.undefined) // Logs `true` to the console.
73+
```
74+
*/
75+
let fromOption: option<'a> => t<'a>

0 commit comments

Comments
 (0)