Railway-oriented programming with trcks.oop
Railway-oriented programming (ROP) is a pattern for composing functions that may fail, keeping error handling clean and explicit. This page covers the object-oriented style: you wrap a value in a trcks.oop.Wrapper subclass, chain method calls to transform it, and then unwrap the result — a "wrap → map → unwrap" mental model. Single-track and double-track code are both discussed. Synchronous and asynchronous code are also discussed.
Prerequisites
- Read Tuple types provided by trcks first; this page assumes familiarity with trcks.Failure, trcks.Success, and trcks.Result.
- The motivation page for ROP explains the design rationale.
- The async sections assume comfort with
async,await, andasyncio.run. - See the glossary for definitions of terms such as "single-track", "double-track", and "short-circuiting".
- See the OOP and FP equivalence table for a side-by-side comparison with the functional style.
Overview matrix
| Runtime and track | Single value | Homogeneous tuple |
|---|---|---|
| Sync and single-track | Wrapper |
TupleWrapper |
| Sync and double-track | ResultWrapper |
ResultTupleWrapper |
| Async and single-track | AwaitableWrapper |
AwaitableTupleWrapper |
| Async and double-track | AwaitableResultWrapper |
AwaitableResultTupleWrapper |
See also
The FP usage page covers the same operations using function composition instead of method chaining.