Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

#### :bug: Bug fix

- Fix result examples. https://github.com/rescript-lang/rescript/pull/7914

#### :memo: Documentation

#### :nail_care: Polish
Expand Down
48 changes: 36 additions & 12 deletions packages/@rescript/runtime/Stdlib_Result.resi
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,16 @@ When `res` is `Error`, returns the error unchanged.
## Examples

```rescript
let asyncSquare = async x => x * x
let square = x => x * x

let result1 = Result.mapOkAsync(Promise.resolve(Ok(4)), asyncSquare) // Returns promise that resolves to Ok(16)
let result2 = Result.mapOkAsync(Promise.resolve(Error("invalid")), asyncSquare) // Returns promise that resolves to Error("invalid")
let result1 = await Result.mapOkAsync(Promise.resolve(Ok(4)), square)
result1 == Ok(16)

let result2 = await Result.mapOkAsync(
Promise.resolve(Error("invalid")),
square,
)
result2 == Error("invalid")
```
*/
let mapOkAsync: (
Expand All @@ -349,10 +355,13 @@ When `res` is `Ok`, returns the ok value unchanged.
## Examples

```rescript
let asyncFormatError = async e => `Error: ${e}`
let formatError = e => `Error: ${e}`

let result1 = Result.mapErrorAsync(Promise.resolve(Ok(42)), asyncFormatError) // Returns promise that resolves to Ok(42)
let result2 = Result.mapErrorAsync(Promise.resolve(Error("invalid")), asyncFormatError) // Returns promise that resolves to Error("Error: invalid")
let result1 = await Result.mapErrorAsync(Promise.resolve(Ok(42)), formatError)
result1 == Ok(42)

let result2 = await Result.mapErrorAsync(Promise.resolve(Error("invalid")), formatError)
result2 == Error("Error: invalid")
```
*/
let mapErrorAsync: (
Expand All @@ -368,15 +377,21 @@ When `res` is `Error`, returns the error unchanged.
## Examples

```rescript
let asyncValidate = async x =>
let asyncValidate = async x =>
if x > 0 {
Ok(x * 2)
} else {
Error("Must be positive")
}

let result1 = Result.flatMapOkAsync(Promise.resolve(Ok(5)), asyncValidate) // Returns promise that resolves to Ok(10)
let result2 = Result.flatMapOkAsync(Promise.resolve(Error("Already failed")), asyncValidate) // Returns promise that resolves to Error("Already failed")
let result1 = await Result.flatMapOkAsync(Promise.resolve(Ok(5)), asyncValidate)
result1 == Ok(10)

let result2 = await Result.flatMapOkAsync(
Promise.resolve(Error("Already failed")),
asyncValidate,
)
result2 == Error("Already failed")
```
*/
let flatMapOkAsync: (
Expand All @@ -392,15 +407,24 @@ When `res` is `Ok`, returns the ok value unchanged.
## Examples

```rescript
let asyncRecover = async error =>
let asyncRecover = async error =>
if error === "timeout" {
Ok("default")
} else {
Error(error)
}

let result1 = Result.flatMapErrorAsync(Promise.resolve(Error("timeout")), asyncRecover) // Returns promise that resolves to Ok("default")
let result2 = Result.flatMapErrorAsync(Promise.resolve(Ok("default")), asyncRecover) // Returns promise that resolves to Ok("default")
let result1 = await Result.flatMapErrorAsync(
Promise.resolve(Error("timeout")),
asyncRecover,
)
result1 == Ok("default")

let result2 = await Result.flatMapErrorAsync(
Promise.resolve(Ok("default")),
asyncRecover,
)
result2 == Ok("default")
```
*/
let flatMapErrorAsync: (
Expand Down
4 changes: 2 additions & 2 deletions tests/docstring_tests/DocTest.res
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ let main = async () => {
} else {
// Let's add the examples inside a Test module because some examples
// have type definitions that are not supported inside a block.
// Also add unit type `()`
// Also add unit type `()`. Also wrap in async to make top-level awaits work.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be controversial, but all tests pass.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume there is no noticeable performance difference from this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope:

master

node scripts/test.js -docstrings  8.49s user 2.25s system 365% cpu 2.938 total

fix-result-examples

node scripts/test.js -docstrings  8.44s user 2.04s system 357% cpu 2.933 total

Some(
`test("${example.name}", () => {
`testAsync("${example.name}", async () => {
module Test = {
${code}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/docstring_tests/DocTest.res.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions tests/docstring_tests/Mocha.res
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
@module("mocha")
external test: (string, unit => unit) => unit = "test"

@module("mocha")
external testAsync: (string, unit => promise<unit>) => unit = "test"

@module("mocha")
external describe: (string, unit => unit) => unit = "describe"
Loading