Skip to content

Commit 0695f09

Browse files
committed
Add kdoc to binding/coroutineBinding
1 parent 520d364 commit 0695f09

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

  • kotlin-result-coroutines/src/commonMain/kotlin/com/github/michaelbull/result/coroutines
  • kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result

kotlin-result-coroutines/src/commonMain/kotlin/com/github/michaelbull/result/coroutines/CoroutineBinding.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,29 @@ public class BindingCancellationException : CancellationException(null as String
6464

6565
@BindingDsl
6666
public interface CoroutineBindingScope<E> : CoroutineScope {
67+
68+
/**
69+
* Unwraps this [Result], returning the [value][Result.value] if [Ok], or short-circuiting
70+
* the enclosing [coroutineBinding] block with the [error][Result.error] if [Err].
71+
*
72+
* Unlike [BindingScope.bind][com.github.michaelbull.result.BindingScope.bind], this
73+
* function also [cancels][CoroutineScope.cancel] the enclosing [CoroutineScope], cancelling
74+
* all other child coroutines.
75+
*
76+
* This is functionally equivalent to Rust's
77+
* [`?` operator](https://doc.rust-lang.org/std/result/index.html#the-question-mark-operator-).
78+
*
79+
* ```
80+
* suspend fun provideX(): Result<Int, ExampleErr> { ... }
81+
* suspend fun provideY(): Result<Int, ExampleErr> { ... }
82+
*
83+
* val result: Result<Int, ExampleErr> = coroutineBinding {
84+
* val x = provideX().bind()
85+
* val y = provideY().bind()
86+
* x + y
87+
* }
88+
* ```
89+
*/
6790
public suspend fun <V> Result<V, E>.bind(): V
6891
}
6992

kotlin-result/src/commonMain/kotlin/com/github/michaelbull/result/Binding.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,25 @@ public expect class BindingException() : Exception
4646

4747
@BindingDsl
4848
public interface BindingScope<E> {
49+
50+
/**
51+
* Unwraps this [Result], returning the [value][Result.value] if [Ok], or short-circuiting
52+
* the enclosing [binding] block with the [error][Result.error] if [Err].
53+
*
54+
* This is functionally equivalent to Rust's
55+
* [`?` operator](https://doc.rust-lang.org/std/result/index.html#the-question-mark-operator-).
56+
*
57+
* ```
58+
* fun provideX(): Result<Int, ExampleErr> { ... }
59+
* fun provideY(): Result<Int, ExampleErr> { ... }
60+
*
61+
* val result: Result<Int, ExampleErr> = binding {
62+
* val x = provideX().bind()
63+
* val y = provideY().bind()
64+
* x + y
65+
* }
66+
* ```
67+
*/
4968
public fun <V> Result<V, E>.bind(): V
5069
}
5170

0 commit comments

Comments
 (0)