These — inclusive OR
Most operations produce one of two outcomes: a value or a failure. These<E, A> is for the cases that don’t fit that model — when an operation partially succeeds, or succeeds but has something worth noting alongside the result. Where Result<E, A> is either an error or a value, These<E, A> has three variants:
Err(e)— only an error, no valueOk(a)— only a value, no errorBoth(e, a)— an error and a value simultaneously
The Both case is what makes These distinct. It represents partial success: the operation produced a result, but there’s also something worth noting — a warning, a degradation, a recovered fallback.
When Result isn’t enough
Section titled “When Result isn’t enough”Result is binary: you either succeed or fail. But some operations produce a meaningful result and a diagnostic at the same time:
- Parsing a number from a string with extra whitespace: the number is valid, but the input was malformed
- A migration that completed with some rows skipped
- An API response that returned data but also included deprecation warnings
- A computation that succeeded using a fallback value when the primary source was unavailable
In these cases, returning Err discards the result. Returning Ok discards the warning. Both holds both.
Creating These values
Section titled “Creating These values”A typical use: a parser that’s lenient but reports what it fixed:
Transforming values
Section titled “Transforming values”map transforms the value in Ok and Both, leaving Err untouched. In Both, the warning is preserved:
mapErr transforms the error/warning in Err and Both, leaving Ok untouched:
bimap transforms both sides at once:
Chaining
Section titled “Chaining”chain passes the value to the next step. The key behaviour is in the Both case: if the current state is Both(e, a) and the next step returns Ok(b), the warning e is preserved in a Both(e, b). The warning travels forward:
If the next step itself returns Both or Err, that result takes precedence and the original warning from Both is dropped.
Extracting the value
Section titled “Extracting the value”match — handle all three cases. Required to cover all variants:
fold — same with positional arguments:
getOrElse — returns the value from Ok or Both, or a fallback for Err:
Type guards
Section titled “Type guards”For checking the variant directly:
hasValue and hasError are useful when you care about the presence of each side independently, without needing to distinguish the exact variant.
Converting to other types
Section titled “Converting to other types”toResult — converts to Result, discarding any warning from Both:
toOption — keeps only the value side:
swap — flips error and value roles:
When to use These vs Result
Section titled “When to use These vs Result”Use These when:
- An operation can succeed and produce a diagnostic simultaneously
- You need to propagate warnings through a pipeline without losing the result
- You’re building lenient parsers or processors that collect notices alongside output
Use Result when:
- An operation either succeeds or fails — there’s no meaningful “partial” state
- You don’t need to carry diagnostics alongside successful values
These is the less commonly reached-for type in the family. When you find yourself wanting to attach metadata, notes, or warnings to a successful result rather than just returning the result alone, that’s the signal to reach for it.