These — inclusive OR
Most operations produce one of two outcomes. These<A, B> is for the cases where both can exist
at once. Where Result<E, A> is either an error or a value, These<A, B> has three variants:
First(a)— only a first valueSecond(b)— only a second valueBoth(a, b)— both a first and a second value simultaneously
Neither side carries a success or failure connotation. These is a neutral inclusive-OR pair:
any combination is valid, and neither side is privileged.
When two sides coexist
Section titled “When two sides coexist”Some operations naturally produce two pieces of information at once:
- Parsing a number from a string with extra whitespace: the number is valid, and the input was malformed
- A migration that completed with some rows skipped
- A computation that produced a result alongside a diagnostic notice
In these cases, discarding either piece loses information. Both holds them together.
Creating These values
Section titled “Creating These values”A typical use: a parser that’s lenient but records what it fixed:
Transforming values
Section titled “Transforming values”mapFirst transforms the first value in First and Both, leaving Second untouched:
mapSecond transforms the second value in Second and Both, leaving First untouched:
mapBoth transforms both sides at once:
Chaining
Section titled “Chaining”chainFirst passes the first value to the next step, leaving Second unchanged. For Both,
the second value is not preserved — the result of f is returned directly:
chainSecond is the symmetric operation on the second side:
Extracting values
Section titled “Extracting values”match — handle all three cases. fold is the positional form if you’d rather not name them:
getFirstOrElse — returns the first value from First or Both, or a fallback for Second.
The fallback can be a different type, widening the result to A | C:
getSecondOrElse — symmetric: returns the second value or a fallback for First. The fallback
can be a different type, widening the result to B | D:
Type guards
Section titled “Type guards”For checking the variant directly:
Utilities
Section titled “Utilities”swap — flips first and second roles:
tap — run a side effect on the first value without changing the These:
When to use These
Section titled “When to use These”Use These when:
- An operation can produce two pieces of information simultaneously
- You need to carry two independent values — where either or both may be present — through a pipeline
- Neither side represents a “primary” success or failure path; both are equally valid
These is the less commonly reached-for type in the family. When you find yourself wanting to
carry two independent pieces of data where any combination is possible, that’s the signal to
reach for it. If you’re unsure whether you need it, you probably don’t — reach for Result or
Maybe first and only switch to These when one of those would lose information you need to keep.
One thing to watch out for: chainFirst on a Both does not carry the second value forward — the
result of f is returned directly. If you need the second value preserved through a chain, Both
is not a transparent container for it.