Skip to content

Commit 112ebd5

Browse files
authored
add comparison fn for Error in Result.equal and compare (#7933)
* add Error comparison fn in Result.equal and compare * rename fOk, fError to eqOk, eqError, cmpOk, cmpError
1 parent 108ca65 commit 112ebd5

File tree

5 files changed

+47
-37
lines changed

5 files changed

+47
-37
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#### :boom: Breaking Change
1616

1717
- Removed "rescript legacy" subcommand in favor of separate "rescript-legacy" binary. https://github.com/rescript-lang/rescript/pull/7928
18+
- Add comparison fn for Error in Result.equal and compare. https://github.com/rescript-lang/rescript/pull/7933
1819

1920
#### :eyeglasses: Spec Compliance
2021

packages/@rescript/runtime/Stdlib_Result.res

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,20 @@ let isError = x =>
7777
| Error(_) => true
7878
}
7979

80-
let equal = (a, b, f) =>
80+
let equal = (a, b, eqOk, eqError) =>
8181
switch (a, b) {
82-
| (Ok(a), Ok(b)) => f(a, b)
82+
| (Ok(a), Ok(b)) => eqOk(a, b)
8383
| (Error(_), Ok(_))
8484
| (Ok(_), Error(_)) => false
85-
| (Error(_), Error(_)) => true
85+
| (Error(a), Error(b)) => eqError(a, b)
8686
}
8787

88-
let compare = (a, b, f) =>
88+
let compare = (a, b, cmpOk, cmpError) =>
8989
switch (a, b) {
90-
| (Ok(a), Ok(b)) => f(a, b)
90+
| (Ok(a), Ok(b)) => cmpOk(a, b)
9191
| (Error(_), Ok(_)) => Stdlib_Ordering.less
9292
| (Ok(_), Error(_)) => Stdlib_Ordering.greater
93-
| (Error(_), Error(_)) => Stdlib_Ordering.equal
93+
| (Error(a), Error(b)) => cmpError(a, b)
9494
}
9595

9696
let forEach = (r, f) =>

packages/@rescript/runtime/Stdlib_Result.resi

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,11 @@ let isOk: result<'a, 'b> => bool
172172
let isError: result<'a, 'b> => bool
173173

174174
/**
175-
`equal(res1, res2, f)`: Determine if two `Result` variables are equal with
176-
respect to an equality function. If `res1` and `res2` are of the form `Ok(n)`
177-
and `Ok(m)`, return the result of `f(n, m)`. If one of `res1` and `res2` are of
175+
`equal(res1, res2, eqOk, eqError)`: Determine if two `Result` variables are equal with
176+
respect to equality functions. If `res1` and `res2` are of the form `Ok(n)`
177+
and `Ok(m)`, return the result of `eqOk(n, m)`. If one of `res1` and `res2` are of
178178
the form `Error(e)`, return false If both `res1` and `res2` are of the form
179-
`Error(e)`, return true
179+
`Error(e)`, return the result of `eqError(e1, e2)`.
180180
181181
## Examples
182182
@@ -191,28 +191,28 @@ let bad2 = Error("really invalid")
191191
192192
let mod10equal = (a, b) => mod(a, 10) === mod(b, 10)
193193
194-
Result.equal(good1, good2, mod10equal) == true
194+
Result.equal(good1, good2, mod10equal, String.equal) == true
195195
196-
Result.equal(good1, bad1, mod10equal) == false
196+
Result.equal(good1, bad1, mod10equal, String.equal) == false
197197
198-
Result.equal(bad2, good2, mod10equal) == false
198+
Result.equal(bad2, good2, mod10equal, String.equal) == false
199199
200-
Result.equal(bad1, bad2, mod10equal) == true
200+
Result.equal(bad1, bad2, mod10equal, String.equal) == false
201201
```
202202
*/
203-
let equal: (result<'a, 'c>, result<'b, 'd>, ('a, 'b) => bool) => bool
203+
let equal: (result<'a, 'c>, result<'b, 'd>, ('a, 'b) => bool, ('c, 'd) => bool) => bool
204204

205205
/**
206-
`compare(res1, res2, f)`: Compare two `Result` variables with respect to a
206+
`compare(res1, res2, cmpOk, cmpError)`: Compare two `Result` variables with respect to a
207207
comparison function. The comparison function returns -1. if the first variable
208208
is "less than" the second, 0. if the two variables are equal, and 1. if the first
209209
is "greater than" the second.
210210
211211
If `res1` and `res2` are of the form `Ok(n)` and `Ok(m)`, return the result of
212-
`f(n, m)`. If `res1` is of the form `Error(e)` and `res2` of the form `Ok(n)`,
212+
`cmpOk(n, m)`. If `res1` is of the form `Error(e)` and `res2` of the form `Ok(n)`,
213213
return -1. (nothing is less than something) If `res1` is of the form `Ok(n)` and
214-
`res2` of the form `Error(e)`, return 1. (something is greater than nothing) If
215-
both `res1` and `res2` are of the form `Error(e)`, return 0. (equal)
214+
`res2` is of the form `Error(e)`, return 1. (something is greater than nothing) If
215+
both `res1` and `res2` are of the form `Error(e)`, return cmpError(e1, e2).
216216
217217
## Examples
218218
@@ -227,18 +227,23 @@ let bad2 = Error("really invalid")
227227
228228
let mod10cmp = (a, b) => Int.compare(mod(a, 10), mod(b, 10))
229229
230-
Result.compare(Ok(39), Ok(57), mod10cmp) == 1.
230+
Result.compare(Ok(39), Ok(57), mod10cmp, String.compare) == 1.
231231
232-
Result.compare(Ok(57), Ok(39), mod10cmp) == -1.
232+
Result.compare(Ok(57), Ok(39), mod10cmp, String.compare) == -1.
233233
234-
Result.compare(Ok(39), Error("y"), mod10cmp) == 1.
234+
Result.compare(Ok(39), Error("y"), mod10cmp, String.compare) == 1.
235235
236-
Result.compare(Error("x"), Ok(57), mod10cmp) == -1.
236+
Result.compare(Error("x"), Ok(57), mod10cmp, String.compare) == -1.
237237
238-
Result.compare(Error("x"), Error("y"), mod10cmp) == 0.
238+
Result.compare(Error("x"), Error("y"), mod10cmp, String.compare) == -1.
239239
```
240240
*/
241-
let compare: (result<'a, 'c>, result<'b, 'd>, ('a, 'b) => Stdlib_Ordering.t) => Stdlib_Ordering.t
241+
let compare: (
242+
result<'a, 'c>,
243+
result<'b, 'd>,
244+
('a, 'b) => Stdlib_Ordering.t,
245+
('c, 'd) => Stdlib_Ordering.t,
246+
) => Stdlib_Ordering.t
242247

243248
/**
244249
`forEach(res, f)` runs the provided function `f` on the `Ok` value. If `res` is `Error`, nothing happens.

packages/@rescript/runtime/lib/es6/Stdlib_Result.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,29 +53,31 @@ function isError(x) {
5353
return x.TAG !== "Ok";
5454
}
5555

56-
function equal(a, b, f) {
56+
function equal(a, b, eqOk, eqError) {
5757
if (a.TAG === "Ok") {
5858
if (b.TAG === "Ok") {
59-
return f(a._0, b._0);
59+
return eqOk(a._0, b._0);
6060
} else {
6161
return false;
6262
}
63+
} else if (b.TAG === "Ok") {
64+
return false;
6365
} else {
64-
return b.TAG !== "Ok";
66+
return eqError(a._0, b._0);
6567
}
6668
}
6769

68-
function compare(a, b, f) {
70+
function compare(a, b, cmpOk, cmpError) {
6971
if (a.TAG === "Ok") {
7072
if (b.TAG === "Ok") {
71-
return f(a._0, b._0);
73+
return cmpOk(a._0, b._0);
7274
} else {
7375
return 1;
7476
}
7577
} else if (b.TAG === "Ok") {
7678
return -1;
7779
} else {
78-
return 0;
80+
return cmpError(a._0, b._0);
7981
}
8082
}
8183

packages/@rescript/runtime/lib/js/Stdlib_Result.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,29 +53,31 @@ function isError(x) {
5353
return x.TAG !== "Ok";
5454
}
5555

56-
function equal(a, b, f) {
56+
function equal(a, b, eqOk, eqError) {
5757
if (a.TAG === "Ok") {
5858
if (b.TAG === "Ok") {
59-
return f(a._0, b._0);
59+
return eqOk(a._0, b._0);
6060
} else {
6161
return false;
6262
}
63+
} else if (b.TAG === "Ok") {
64+
return false;
6365
} else {
64-
return b.TAG !== "Ok";
66+
return eqError(a._0, b._0);
6567
}
6668
}
6769

68-
function compare(a, b, f) {
70+
function compare(a, b, cmpOk, cmpError) {
6971
if (a.TAG === "Ok") {
7072
if (b.TAG === "Ok") {
71-
return f(a._0, b._0);
73+
return cmpOk(a._0, b._0);
7274
} else {
7375
return 1;
7476
}
7577
} else if (b.TAG === "Ok") {
7678
return -1;
7779
} else {
78-
return 0;
80+
return cmpError(a._0, b._0);
7981
}
8082
}
8183

0 commit comments

Comments
 (0)