Skip to content

Commit

Permalink
📇 Update tutorial to reflect new void behavior
Browse files Browse the repository at this point in the history
The tutorial for the `result<void,E>` coalescing has been updated to
identify the new behavior with requiring explicit construction.
  • Loading branch information
bitwizeshift committed Dec 16, 2020
1 parent 5987cd0 commit ba864f3
Showing 1 changed file with 6 additions and 20 deletions.
26 changes: 6 additions & 20 deletions doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -395,33 +395,19 @@ template <typename Fn>
auto try_invoke(Fn&& fn) noexcept -> void
{
// Coalesce all results to 'void'
cpp::result<void,E> result = std::invoke(std::forward<Fn>(fn));
auto result = cpp::result<void,E>{
std::invoke(std::forward<Fn>(fn))
};
if (!result) {
// Do something with the error
signal_service::notify_error(result.error());
}
}
```

This erasure also works in terms of other utilities as well. In the same way
that `std::function` can erase any `R` return type to `void`, the implicit
conversions also allow for any `result<R,E>` return type to
`result<void,E>`:

```cpp

auto register_handler(std::function<cpp::result<void,E>()> fn) -> void;

/* ... */

auto int_handler() -> result<int,E>;
auto string_handler() -> result<std::string,E>;

/* ... */

register_handler(&int_handler);
register_handler(&string_handler);
```
**Note:** Erasure with `result<void,E>` requires explicit construction for both
construction and assignment, since `result<void,E>` is meant to model the
behavior and semantics of a `void` cast, which requires explicitness.

### `failure` with references

Expand Down

0 comments on commit ba864f3

Please sign in to comment.