Skip to content

Commit 664812e

Browse files
aspeddrozth
authored andcommitted
Add Nullable.isNullable (rescript-lang#227)
* add `Nullable.isNullable` * update CHANGELOG.md --------- Co-authored-by: Gabriel Nordeborn <[email protected]> (cherry picked from commit d68e2be)
1 parent 05260fb commit 664812e

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- BREAKING: Fixes the type of `RegExp.Result.t` to be `array<option<string>>` instead of `array<string>`. https://github.com/rescript-association/rescript-core/pull/233.
66
- BREAKING: Adds typed bindings to `Intl`, replacing the options type of `{..}` with records. https://github.com/rescript-association/rescript-core/pull/65
77
- BREAKING: Align List api with other modules (`List.getBy` -> `List.find` etc.). https://github.com/rescript-association/rescript-core/pull/195
8+
- Add `Nullable.isNullable` function. https://github.com/rescript-association/rescript-core/pull/227
89
- Remove some deps to Belt, Pervasives and Js. https://github.com/rescript-association/rescript-core/pull/226/commits
910
- Fix: Expose Intl.Common. https://github.com/rescript-association/rescript-core/pull/197
1011
- Add `Array.join` and deprecate `Array.joinWith`. https://github.com/rescript-association/rescript-core/pull/205

src/Core__Nullable.res

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ external null: t<'a> = "#null"
44

55
external undefined: t<'a> = "#undefined"
66

7+
external isNullable: t<'a> => bool = "#is_nullable"
8+
79
external make: 'a => t<'a> = "%identity"
810

911
external toOption: t<'a> => option<'a> = "#nullable_to_opt"

src/Core__Nullable.resi

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,27 @@ Console.log(undefined) // Logs `undefined` to the console.
3434
*/
3535
external undefined: t<'a> = "#undefined"
3636

37+
/**
38+
`isNullable(a)` returns `true` if `a` is null or undefined, `false` otherwise.
39+
40+
## Examples
41+
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+
// Check if asNullable is not null or undefined
50+
switch asNullable->Nullable.isNullable {
51+
| true => assert(false)
52+
| false => assert(true)
53+
}
54+
```
55+
*/
56+
external isNullable: t<'a> => bool = "#is_nullable"
57+
3758
/**
3859
Creates a new nullable value from the provided value.
3960
This means the compiler will enforce null checks for the new value.

0 commit comments

Comments
 (0)