Move binding type declarations to lefthand of callsite

This commit is contained in:
Michael Bull 2024-03-05 20:23:07 +00:00
parent f7c9f605f1
commit d8ea6e1c04
6 changed files with 23 additions and 27 deletions

View File

@ -187,7 +187,7 @@ suspend fun failsIn5ms(): Result<Int, DomainErrorA> { ... }
suspend fun failsIn1ms(): Result<Int, DomainErrorB> { ... } suspend fun failsIn1ms(): Result<Int, DomainErrorB> { ... }
runBlocking { runBlocking {
val result = binding<Int, BindingError> { val result: Result<Int, BindingError> = binding {
val x = async { failsIn5ms().bind() } val x = async { failsIn5ms().bind() }
val y = async { failsIn1ms().bind() } val y = async { failsIn1ms().bind() }
x.await() + y.await() x.await() + y.await()

View File

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

View File

@ -42,29 +42,25 @@ class SuspendBindingBenchmark {
private val time = 100L private val time = 100L
private fun nonSuspend() = binding<Int, Error> { private fun nonSuspend(): Result<Int, Error> = binding {
val x = provideXBlocking().bind() val x = provideXBlocking().bind()
val y = provideYBlocking().bind() val y = provideYBlocking().bind()
x + y x + y
} }
private suspend fun withSuspend(): Result<Int, Error> { private suspend fun withSuspend(): Result<Int, Error> = coroutineBinding {
return coroutineBinding {
val x = provideX().bind() val x = provideX().bind()
val y = provideY().bind() val y = provideY().bind()
x + y x + y
} }
}
private suspend fun withAsyncSuspend(): Result<Int, Error> { private suspend fun withAsyncSuspend(): Result<Int, Error> = coroutineScope {
return coroutineScope {
coroutineBinding { coroutineBinding {
val x = async { provideX().bind() } val x = async { provideX().bind() }
val y = async { provideY().bind() } val y = async { provideY().bind() }
x.await() + y.await() x.await() + y.await()
} }
} }
}
private fun provideXBlocking(): Result<Int, Error> { private fun provideXBlocking(): Result<Int, Error> {
Thread.sleep(time) Thread.sleep(time)

View File

@ -28,7 +28,7 @@ class SuspendableBindingTest {
return Ok(2) return Ok(2)
} }
val result = binding<Int, BindingError> { val result: Result<Int, BindingError> = binding {
val x = provideX().bind() val x = provideX().bind()
val y = provideY().bind() val y = provideY().bind()
x + y x + y
@ -52,7 +52,7 @@ class SuspendableBindingTest {
return Ok(x + 2) return Ok(x + 2)
} }
val result = binding<Int, BindingError> { val result: Result<Int, BindingError> = binding {
val x = provideX().bind() val x = provideX().bind()
val y = provideY(x.toInt()).bind() val y = provideY(x.toInt()).bind()
y y
@ -81,7 +81,7 @@ class SuspendableBindingTest {
return Ok(2) return Ok(2)
} }
val result = binding<Int, BindingError> { val result: Result<Int, BindingError> = binding {
val x = provideX().bind() val x = provideX().bind()
val y = provideY().bind() val y = provideY().bind()
val z = provideZ().bind() val z = provideZ().bind()
@ -118,7 +118,7 @@ class SuspendableBindingTest {
return Err(BindingError) return Err(BindingError)
} }
val result = binding<Int, BindingError> { val result: Result<Int, BindingError> = binding {
val x = provideX().bind() val x = provideX().bind()
val y = provideY().bind() val y = provideY().bind()
val z = provideZ().bind() val z = provideZ().bind()
@ -152,7 +152,7 @@ class SuspendableBindingTest {
return Ok(2) return Ok(2)
} }
val result = binding<Int, BindingError> { val result: Result<Int, BindingError> = binding {
val x = provideX().bind() val x = provideX().bind()
val y = provideY().bind() val y = provideY().bind()
val z = provideZ().bind() val z = provideZ().bind()

View File

@ -34,7 +34,7 @@ class AsyncSuspendableBindingTest {
return Ok(2) return Ok(2)
} }
val result = binding<Int, BindingError> { val result: Result<Int, BindingError> = binding {
val x = async { provideX().bind() } val x = async { provideX().bind() }
val y = async { provideY().bind() } val y = async { provideY().bind() }
x.await() + y.await() x.await() + y.await()
@ -63,7 +63,7 @@ class AsyncSuspendableBindingTest {
return Err(BindingError.BindingErrorB) return Err(BindingError.BindingErrorB)
} }
val result = binding<Int, BindingError> { val result: Result<Int, BindingError> = binding {
val x = async { provideX().bind() } val x = async { provideX().bind() }
val y = async { provideY().bind() } val y = async { provideY().bind() }
val z = async { provideZ().bind() } val z = async { provideZ().bind() }
@ -96,7 +96,7 @@ class AsyncSuspendableBindingTest {
val dispatcherA = StandardTestDispatcher(testScheduler) val dispatcherA = StandardTestDispatcher(testScheduler)
val dispatcherB = StandardTestDispatcher(testScheduler) val dispatcherB = StandardTestDispatcher(testScheduler)
val result = binding<Int, BindingError> { val result: Result<Int, BindingError> = binding {
val x = async(dispatcherA) { provideX().bind() } val x = async(dispatcherA) { provideX().bind() }
val y = async(dispatcherB) { provideY().bind() } val y = async(dispatcherB) { provideY().bind() }
@ -143,7 +143,7 @@ class AsyncSuspendableBindingTest {
val dispatcherB = StandardTestDispatcher(testScheduler) val dispatcherB = StandardTestDispatcher(testScheduler)
val dispatcherC = StandardTestDispatcher(testScheduler) val dispatcherC = StandardTestDispatcher(testScheduler)
val result = binding<Unit, BindingError> { val result: Result<Unit, BindingError> = binding {
launch(dispatcherA) { provideX().bind() } launch(dispatcherA) { provideX().bind() }
testScheduler.advanceTimeBy(20) testScheduler.advanceTimeBy(20)

View File

@ -66,7 +66,7 @@ class BindingTest {
fun provideY(): Result<String, BindingError> = Err(BindingError) fun provideY(): Result<String, BindingError> = Err(BindingError)
fun provideZ(): Result<Int, BindingError> = Ok(2) fun provideZ(): Result<Int, BindingError> = Ok(2)
val result = binding<Int, BindingError> { val result: Result<Int, BindingError> = binding {
val x = provideX().bind() val x = provideX().bind()
val y = provideY().bind() val y = provideY().bind()
val z = provideZ().bind() val z = provideZ().bind()