Skip to content

Commit

Permalink
Move binding type declarations to lefthand of callsite
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbull committed Mar 5, 2024
1 parent 676a2f3 commit 0ca6421
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ suspend fun failsIn5ms(): Result<Int, DomainErrorA> { ... }
suspend fun failsIn1ms(): Result<Int, DomainErrorB> { ... }

runBlocking {
val result = binding<Int, BindingError> {
val result: Result<Int, BindingError> = binding {
val x = async { failsIn5ms().bind() }
val y = async { failsIn1ms().bind() }
x.await() + y.await()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class BindingBenchmark {

@Benchmark
fun bindingSuccess(blackhole: Blackhole) {
val result = binding<Int, Error> {
val result: Result<Int, Error> = binding {
val x = provideX().bind()
val y = provideY().bind()
x + y
Expand All @@ -27,7 +27,7 @@ class BindingBenchmark {

@Benchmark
fun bindingFailure(blackhole: Blackhole) {
val result = binding<Int, Error> {
val result: Result<Int, Error> = binding {
val x = provideX().bind()
val z = provideZ().bind()
x + z
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,23 @@ class SuspendBindingBenchmark {

private val time = 100L

private fun nonSuspend() = binding<Int, Error> {
private fun nonSuspend(): Result<Int, Error> = binding {
val x = provideXBlocking().bind()
val y = provideYBlocking().bind()
x + y
}

private suspend fun withSuspend(): Result<Int, Error> {
return coroutineBinding {
val x = provideX().bind()
val y = provideY().bind()
x + y
}
private suspend fun withSuspend(): Result<Int, Error> = coroutineBinding {
val x = provideX().bind()
val y = provideY().bind()
x + y
}

private suspend fun withAsyncSuspend(): Result<Int, Error> {
return coroutineScope {
coroutineBinding {
val x = async { provideX().bind() }
val y = async { provideY().bind() }
x.await() + y.await()
}
private suspend fun withAsyncSuspend(): Result<Int, Error> = coroutineScope {
coroutineBinding {
val x = async { provideX().bind() }
val y = async { provideY().bind() }
x.await() + y.await()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class SuspendableBindingTest {
return Ok(2)
}

val result = binding<Int, BindingError> {
val result: Result<Int, BindingError> = binding {
val x = provideX().bind()
val y = provideY().bind()
x + y
Expand All @@ -52,7 +52,7 @@ class SuspendableBindingTest {
return Ok(x + 2)
}

val result = binding<Int, BindingError> {
val result: Result<Int, BindingError> = binding {
val x = provideX().bind()
val y = provideY(x.toInt()).bind()
y
Expand Down Expand Up @@ -81,7 +81,7 @@ class SuspendableBindingTest {
return Ok(2)
}

val result = binding<Int, BindingError> {
val result: Result<Int, BindingError> = binding {
val x = provideX().bind()
val y = provideY().bind()
val z = provideZ().bind()
Expand Down Expand Up @@ -118,7 +118,7 @@ class SuspendableBindingTest {
return Err(BindingError)
}

val result = binding<Int, BindingError> {
val result: Result<Int, BindingError> = binding {
val x = provideX().bind()
val y = provideY().bind()
val z = provideZ().bind()
Expand Down Expand Up @@ -152,7 +152,7 @@ class SuspendableBindingTest {
return Ok(2)
}

val result = binding<Int, BindingError> {
val result: Result<Int, BindingError> = binding {
val x = provideX().bind()
val y = provideY().bind()
val z = provideZ().bind()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class AsyncSuspendableBindingTest {
return Ok(2)
}

val result = binding<Int, BindingError> {
val result: Result<Int, BindingError> = binding {
val x = async { provideX().bind() }
val y = async { provideY().bind() }
x.await() + y.await()
Expand Down Expand Up @@ -63,7 +63,7 @@ class AsyncSuspendableBindingTest {
return Err(BindingError.BindingErrorB)
}

val result = binding<Int, BindingError> {
val result: Result<Int, BindingError> = binding {
val x = async { provideX().bind() }
val y = async { provideY().bind() }
val z = async { provideZ().bind() }
Expand Down Expand Up @@ -96,7 +96,7 @@ class AsyncSuspendableBindingTest {
val dispatcherA = StandardTestDispatcher(testScheduler)
val dispatcherB = StandardTestDispatcher(testScheduler)

val result = binding<Int, BindingError> {
val result: Result<Int, BindingError> = binding {
val x = async(dispatcherA) { provideX().bind() }
val y = async(dispatcherB) { provideY().bind() }

Expand Down Expand Up @@ -143,7 +143,7 @@ class AsyncSuspendableBindingTest {
val dispatcherB = StandardTestDispatcher(testScheduler)
val dispatcherC = StandardTestDispatcher(testScheduler)

val result = binding<Unit, BindingError> {
val result: Result<Unit, BindingError> = binding {
launch(dispatcherA) { provideX().bind() }

testScheduler.advanceTimeBy(20)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class BindingTest {
fun provideY(): Result<String, BindingError> = Err(BindingError)
fun provideZ(): Result<Int, BindingError> = Ok(2)

val result = binding<Int, BindingError> {
val result: Result<Int, BindingError> = binding {
val x = provideX().bind()
val y = provideY().bind()
val z = provideZ().bind()
Expand Down

0 comments on commit 0ca6421

Please sign in to comment.