How michaelbull/kotlin-result works
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: Result.kt Result representation · Binding.kt Binding DSL · kotlin-result/build.gradle.kts Core Gradle module · kotlin-result-coroutines/build.gradle.kts Coroutine Gradle module
Components 19
Core artifact
- Result representation · Canonical Ok/Err representation and core extensions.
kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Result.kt - Factories and capture · Creates results and converts nullable or thrown values.
kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Factory.kt - Transformations · Maps success, error, or both branches.
kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Map.kt - Short-circuit composition · Chains successful results and recovers errors.
kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/And.kt - Terminal extraction · Separates unsafe result extraction from transformations.
kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Unwrap.kt - Binding DSL · Turns imperative blocks into Result composition.
kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Binding.kt - Binding failure mechanism · JVM-specific control-flow mechanism; parallel target implementations exist.
kotlin-result/src/jvmMain/kotlin/com/github/michaelbull/result/BindingException.kt - Zip and validation · Combines independent results or accumulates validation errors.
kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Zip.kt - Collection operations · Aggregates, partitions, and filters result collections.
kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Iterable.kt - Core Gradle module
kotlin-result/build.gradle.kts
Optional coroutine artifact
- Coroutine binding · Cancels sibling work when a bound result fails.
kotlin-result-coroutines/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/CoroutineBinding.kt - Suspend capture · Rethrows cooperative cancellation.
kotlin-result-coroutines/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/RunSuspendCatching.kt - Flow operators · Adapts Result collection operations to Flow.
kotlin-result-coroutines/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/flow/Flow.kt - Parallel zip
kotlin-result-coroutines/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/ParZip.kt - Coroutine Gradle module
kotlin-result-coroutines/build.gradle.kts
Build and delivery
- Multi-module Gradle build · Defines modules, targets, and shared conventions.
settings.gradle.kts - Publishing conventions · Shared Maven publication setup.
buildSrc/src/main/kotlin/publish-conventions.gradle.kts
Consumers
- Customer-service example · Demonstrates library composition.
example/src/main/kotlin/com/github/michaelbull/result/example/Application.kt - Binding benchmarks · Compares binding approaches including Arrow and coroutines.
benchmarks/build.gradle.kts
Connections 18
- 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