# michaelbull/kotlin-result architecture

> Architecture diagram of the GitHub repository michaelbull/kotlin-result, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/michaelbull/kotlin-result
- Repository: https://github.com/michaelbull/kotlin-result

## Overview

#### Purpose and entry points
This is a Kotlin Multiplatform library for typed success/failure values (`Result<V, E>`), published as `kotlin-result`, plus an optional coroutine artifact. Consumer-facing entry points are top-level `Ok`/`Err`, `Result`, and extension APIs in `kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Result.kt` and `kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Factory.kt`.

#### Module topology
- **Core artifact:** `kotlin-result/`, configured by `kotlin-result/build.gradle.kts`; all common behavior resides in `commonMain`.
- **Coroutine artifact:** `kotlin-result-coroutines/`, configured by `kotlin-result-coroutines/build.gradle.kts`, extends core result handling with suspend, concurrency, and Flow operators.
- **Consumer demonstration:** `example/src/main/kotlin/com/github/michaelbull/result/example/Application.kt` composes the library into a customer-service application.
- **Performance comparison:** `benchmarks/` compares binding implementations, including Arrow and coroutines.

#### Core representation and platform boundary
`Result` is an inline value-class-oriented abstraction representing either a value or error, designed to avoid happy-path allocations. Its canonical representation and constructors are in `kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Result.kt`. Common API behavior is platform-neutral; only binding failure mechanics differ by target through `kotlin-result/src/jvmMain/kotlin/com/github/michaelbull/result/BindingException.kt`, with parallel JS, Native, and Wasm implementations.

#### Result transformation pipeline
The primary control flow is railway-style propagation: successful values continue through transformations while errors bypass success callbacks.
- Creation/capture and nullable conversion: `kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Factory.kt`.
- Success/error/both-side transformations: `kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Map.kt`.
- Sequential short-circuit composition and recovery: `kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/And.kt`, `kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Or.kt`.
- Terminal extraction is deliberately separated into `kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Unwrap.kt`; unsafe access is marked by annotations under `annotation/`.

#### Multi-step and aggregate operations
`binding` converts imperative-looking blocks into short-circuiting Result composition, preserving intermediate values; implementation is in `kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Binding.kt`. Independent result producers are combined by `zip` or validation-error accumulation in `kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Zip.kt`. Collection-level aggregation, partitioning, and filtering are defined in `kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Iterable.kt`.

#### Coroutine and stream layer
The optional coroutine module is a dependency boundary: consumers opt in rather than imposing `kotlinx.coroutines` on core users. `kotlin-result-coroutines/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/CoroutineBinding.kt` runs concurrent work in a coroutine scope and cancels sibling work when a bound result fails. `RunSuspendCatching.kt` preserves cooperative cancellation by rethrowing `CancellationException`. `flow/Flow.kt` adapts Result collection operations to `Flow<Result<V,E>>`, while `ParZip.kt` supports parallel combination.

#### Build, targets, and delivery
Gradle Kotlin DSL composes the multi-module build from `settings.gradle.kts` and root `build.gradle.kts`; shared publishing and Kotlin conventions live in `buildSrc/src/main/kotlin/publish-conventions.gradle.kts` and `buildSrc/src/main/kotlin/kotlin-conventions.gradle.kts`. Supported runtimes include JVM/Android, JS/Node, Kotlin/Native targets, and Wasm JS. Distribution is through Maven Central; CI is GitHub Actions at `.github/workflows/ci.yaml`.

Main entry points: [`kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Result.kt`](https://github.com/michaelbull/kotlin-result/blob/master/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Result.kt) (Result representation), [`kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Binding.kt`](https://github.com/michaelbull/kotlin-result/blob/master/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Binding.kt) (Binding DSL), [`kotlin-result/build.gradle.kts`](https://github.com/michaelbull/kotlin-result/blob/master/kotlin-result/build.gradle.kts) (Core Gradle module), [`kotlin-result-coroutines/build.gradle.kts`](https://github.com/michaelbull/kotlin-result/blob/master/kotlin-result-coroutines/build.gradle.kts) (Coroutine Gradle module)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["Core artifact"]
  node_result["Result representation<br/>inline result API<br/>[Result.kt]"]
  node_factory["Factories and capture<br/>creation API<br/>[Factory.kt]"]
  node_map["Transformations<br/>mapping API<br/>[Map.kt]"]
  node_composition["Short-circuit composition<br/>and/or API<br/>[And.kt]"]
  node_unwrap["Terminal extraction<br/>unwrap API<br/>[Unwrap.kt]"]
  node_binding["Binding DSL<br/>short-circuit DSL<br/>[Binding.kt]"]
  node_binding_exception["Binding failure mechanism<br/>platform implementation"]
  node_zip["Zip and validation<br/>aggregate API<br/>[Zip.kt]"]
  node_iterable["Collection operations<br/>collection API<br/>[Iterable.kt]"]
  node_core_build["Core Gradle module<br/>build module<br/>[build.gradle.kts]"]
end

subgraph group_coroutines["Optional coroutine artifact"]
  node_coroutine_binding["Coroutine binding<br/>concurrent binding API"]
  node_suspend_catching["Suspend capture<br/>suspend API"]
  node_flow["Flow operators<br/>stream API<br/>[Flow.kt]"]
  node_par_zip["Parallel zip<br/>parallel API<br/>[ParZip.kt]"]
  node_coroutines_build["Coroutine Gradle module<br/>build module<br/>[build.gradle.kts]"]
end

subgraph group_delivery["Build and delivery"]
  node_gradle_build["Multi-module Gradle build<br/>build orchestration"]
  node_publishing["Publishing conventions<br/>delivery convention"]
end

subgraph group_usage["Consumers"]
  node_example["Customer-service example<br/>consumer application<br/>[Application.kt]"]
  node_benchmarks["Binding benchmarks<br/>performance suite<br/>[build.gradle.kts]"]
end

node_factory -->|"creates"| node_result
node_map -->|"transforms"| node_result
node_composition -->|"chains"| node_result
node_unwrap -->|"extracts"| node_result
node_binding -->|"composes"| node_result
node_binding -.->|"uses target failure control flow"| node_binding_exception
node_zip -->|"combines"| node_result
node_iterable -->|"aggregates"| node_result
node_coroutine_binding -->|"extends"| node_binding
node_suspend_catching -->|"captures into"| node_result
node_flow -->|"adapts"| node_iterable
node_par_zip -->|"parallelizes"| node_zip
node_coroutines_build -->|"depends on"| node_core_build
node_core_build -->|"included by"| node_gradle_build
node_coroutines_build -->|"included by"| node_gradle_build
node_publishing -.->|"configures publication"| node_core_build
node_example -->|"consumes"| node_result
node_benchmarks -->|"measures"| node_binding

click node_result "https://github.com/michaelbull/kotlin-result/blob/master/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Result.kt"
click node_factory "https://github.com/michaelbull/kotlin-result/blob/master/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Factory.kt"
click node_map "https://github.com/michaelbull/kotlin-result/blob/master/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Map.kt"
click node_composition "https://github.com/michaelbull/kotlin-result/blob/master/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/And.kt"
click node_unwrap "https://github.com/michaelbull/kotlin-result/blob/master/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Unwrap.kt"
click node_binding "https://github.com/michaelbull/kotlin-result/blob/master/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Binding.kt"
click node_binding_exception "https://github.com/michaelbull/kotlin-result/blob/master/kotlin-result/src/jvmMain/kotlin/com/github/michaelbull/result/BindingException.kt"
click node_zip "https://github.com/michaelbull/kotlin-result/blob/master/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Zip.kt"
click node_iterable "https://github.com/michaelbull/kotlin-result/blob/master/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Iterable.kt"
click node_core_build "https://github.com/michaelbull/kotlin-result/blob/master/kotlin-result/build.gradle.kts"
click node_coroutine_binding "https://github.com/michaelbull/kotlin-result/blob/master/kotlin-result-coroutines/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/CoroutineBinding.kt"
click node_suspend_catching "https://github.com/michaelbull/kotlin-result/blob/master/kotlin-result-coroutines/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/RunSuspendCatching.kt"
click node_flow "https://github.com/michaelbull/kotlin-result/blob/master/kotlin-result-coroutines/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/flow/Flow.kt"
click node_par_zip "https://github.com/michaelbull/kotlin-result/blob/master/kotlin-result-coroutines/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/ParZip.kt"
click node_coroutines_build "https://github.com/michaelbull/kotlin-result/blob/master/kotlin-result-coroutines/build.gradle.kts"
click node_gradle_build "https://github.com/michaelbull/kotlin-result/blob/master/settings.gradle.kts"
click node_publishing "https://github.com/michaelbull/kotlin-result/blob/master/buildSrc/src/main/kotlin/publish-conventions.gradle.kts"
click node_example "https://github.com/michaelbull/kotlin-result/blob/master/example/src/main/kotlin/com/github/michaelbull/result/example/Application.kt"
click node_benchmarks "https://github.com/michaelbull/kotlin-result/blob/master/benchmarks/build.gradle.kts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_result,node_factory,node_map,node_composition,node_unwrap,node_binding,node_binding_exception,node_zip,node_iterable,node_core_build toneBlue
class node_coroutine_binding,node_suspend_catching,node_flow,node_par_zip,node_coroutines_build toneAmber
class node_gradle_build,node_publishing toneMint
class node_example,node_benchmarks toneRose
```

## Components

### Core artifact

- [Result representation](https://github.com/michaelbull/kotlin-result/blob/master/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Result.kt): inline result API · Canonical Ok/Err representation and core extensions. · `kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Result.kt`
- [Factories and capture](https://github.com/michaelbull/kotlin-result/blob/master/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Factory.kt): creation API · Creates results and converts nullable or thrown values. · `kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Factory.kt`
- [Transformations](https://github.com/michaelbull/kotlin-result/blob/master/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Map.kt): mapping API · Maps success, error, or both branches. · `kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Map.kt`
- [Short-circuit composition](https://github.com/michaelbull/kotlin-result/blob/master/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/And.kt): and/or API · Chains successful results and recovers errors. · `kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/And.kt`
- [Terminal extraction](https://github.com/michaelbull/kotlin-result/blob/master/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Unwrap.kt): unwrap API · Separates unsafe result extraction from transformations. · `kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Unwrap.kt`
- [Binding DSL](https://github.com/michaelbull/kotlin-result/blob/master/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Binding.kt): short-circuit DSL · Turns imperative blocks into Result composition. · `kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Binding.kt`
- [Binding failure mechanism](https://github.com/michaelbull/kotlin-result/blob/master/kotlin-result/src/jvmMain/kotlin/com/github/michaelbull/result/BindingException.kt): platform implementation · JVM-specific control-flow mechanism; parallel target implementations exist. · `kotlin-result/src/jvmMain/kotlin/com/github/michaelbull/result/BindingException.kt`
- [Zip and validation](https://github.com/michaelbull/kotlin-result/blob/master/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Zip.kt): aggregate API · Combines independent results or accumulates validation errors. · `kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Zip.kt`
- [Collection operations](https://github.com/michaelbull/kotlin-result/blob/master/kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Iterable.kt): collection API · Aggregates, partitions, and filters result collections. · `kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Iterable.kt`
- [Core Gradle module](https://github.com/michaelbull/kotlin-result/blob/master/kotlin-result/build.gradle.kts): build module · `kotlin-result/build.gradle.kts`

### Optional coroutine artifact

- [Coroutine binding](https://github.com/michaelbull/kotlin-result/blob/master/kotlin-result-coroutines/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/CoroutineBinding.kt): concurrent binding API · Cancels sibling work when a bound result fails. · `kotlin-result-coroutines/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/CoroutineBinding.kt`
- [Suspend capture](https://github.com/michaelbull/kotlin-result/blob/master/kotlin-result-coroutines/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/RunSuspendCatching.kt): suspend API · Rethrows cooperative cancellation. · `kotlin-result-coroutines/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/RunSuspendCatching.kt`
- [Flow operators](https://github.com/michaelbull/kotlin-result/blob/master/kotlin-result-coroutines/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/flow/Flow.kt): stream API · Adapts Result collection operations to Flow. · `kotlin-result-coroutines/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/flow/Flow.kt`
- [Parallel zip](https://github.com/michaelbull/kotlin-result/blob/master/kotlin-result-coroutines/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/ParZip.kt): parallel API · `kotlin-result-coroutines/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/ParZip.kt`
- [Coroutine Gradle module](https://github.com/michaelbull/kotlin-result/blob/master/kotlin-result-coroutines/build.gradle.kts): build module · `kotlin-result-coroutines/build.gradle.kts`

### Build and delivery

- [Multi-module Gradle build](https://github.com/michaelbull/kotlin-result/blob/master/settings.gradle.kts): build orchestration · Defines modules, targets, and shared conventions. · `settings.gradle.kts`
- [Publishing conventions](https://github.com/michaelbull/kotlin-result/blob/master/buildSrc/src/main/kotlin/publish-conventions.gradle.kts): delivery convention · Shared Maven publication setup. · `buildSrc/src/main/kotlin/publish-conventions.gradle.kts`

### Consumers

- [Customer-service example](https://github.com/michaelbull/kotlin-result/blob/master/example/src/main/kotlin/com/github/michaelbull/result/example/Application.kt): consumer application · Demonstrates library composition. · `example/src/main/kotlin/com/github/michaelbull/result/example/Application.kt`
- [Binding benchmarks](https://github.com/michaelbull/kotlin-result/blob/master/benchmarks/build.gradle.kts): performance suite · Compares binding approaches including Arrow and coroutines. · `benchmarks/build.gradle.kts`

## Connections

- Factories and capture → Result representation: creates
- Transformations → Result representation: transforms
- Short-circuit composition → Result representation: chains
- Terminal extraction → Result representation: extracts
- Binding DSL → Result representation: composes
- Binding DSL → Binding failure mechanism: uses target failure control flow
- Zip and validation → Result representation: combines
- Collection operations → Result representation: aggregates
- Coroutine binding → Binding DSL: extends
- Suspend capture → Result representation: captures into
- Flow operators → Collection operations: adapts
- Parallel zip → Zip and validation: parallelizes
- Coroutine Gradle module → Core Gradle module: depends on
- Core Gradle module → Multi-module Gradle build: included by
- Coroutine Gradle module → Multi-module Gradle build: included by
- Publishing conventions → Core Gradle module: configures publication
- Customer-service example → Result representation: consumes
- Binding benchmarks → Binding DSL: measures
