Skip to content

Commit

Permalink
Merge pull request #6952 from ageron/improve-result-chaining-doc
Browse files Browse the repository at this point in the history
Improve Result-chaining documentation
  • Loading branch information
smores56 authored Aug 1, 2024
2 parents c64b3d9 + 48bdc86 commit 790eabd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions roc-for-elm-programmers.md
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,8 @@ foo 1 2 if something then 3 else 4

## Backpassing

Note: the backpassing syntax will likely be replaced with a new `?` operator in the future, see [this discussion](https:/roc-lang/roc/issues/6828) for more details.

Suppose I'm using a platform for making a CLI, and I want to run several
`Task`s in a row which read some files from the disk. Here's one way I could do
that, assuming `Task.await` is like Elm's `Task.andThen` with arguments flipped:
Expand Down
14 changes: 14 additions & 0 deletions www/content/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,20 @@ Result.isOk (List.get ["a", "b", "c"] 1)
# Note: There's a Result.isErr function that works similarly.
```

```roc
Result.try (Str.toU64 "2") \idx -> List.get ["a", "b", "c", "d"] idx
# returns `Ok "c"`
# Notes:
# - `Str.toU64 "2"` parses the string "2" to the integer 2, and returns `Ok 2` (more on
# integer types later)
# - since parsing is successful, `Result.try` passes 2 to the function `\idx -> ...`
# - passing "abc" or "1000" instead of "2" would have resulted in `Err InvalidNumStr`
# or `Err OutOfBounds` respectively
```

`Result.try` is often used to chain functions that can fail (as in the example above), returning the first error if any occurs. There is [a discussion](https:/roc-lang/roc/issues/6828) to add syntax sugar for such chaining.

### [Walking the elements in a list](#walking-the-elements-in-a-list) {#walking-the-elements-in-a-list}

We've now seen a few different ways you can transform lists. Sometimes, though, there's nothing
Expand Down

0 comments on commit 790eabd

Please sign in to comment.