Marc
10/25/2023, 7:19 AMclass MainSyncRepository<F, K, Q : Get<K>, A>(
private val mainDataSource: DataSource<F, Q, A>,
private val storeDataSource: StoreDataSource<F, K, A>,
private val fallbackChecks: List<F> = emptyList()
) : Repository<F, Q, A> {
override suspend fun invoke(q: Q): Either<F, A> =
mainDataSource(q)
.flatMap { storeDataSource(Put(q.key, Some(it))) }
.handleErrorWith { f ->
if (f in fallbackChecks) {
storeDataSource(q)
.mapLeft { f }
} else {
Either.Left(f)
}
}
}
and I’d like to translate it with context receivers (this is like i would wrap the whole invoke function in a either
dsl for instance.
How would i manage to translate the handleErrorWith
? cc @simon.vergauwenAlejandro Serrano.Mena
10/25/2023, 7:20 AMrecover
for thisMarc
10/25/2023, 7:21 AMAlejandro Serrano.Mena
10/25/2023, 7:29 AMoverride suspend fun invoke(q: Q): Either<F, A> = either {
recover({
val source = mainDataSource(q).bind()
storeDataSource(Put(q.key, Some(source))).bind()
}) { f ->
if (f in fallbackChecks) withError({ f }) { storeDataSource(q).bind() }
else raise(f)
}
}
Marc
10/25/2023, 7:35 AMOption
lost bind
in Arrow 1.2.0?Alejandro Serrano.Mena
10/25/2023, 7:36 AMbind
everywhere, only in those places in which you were to provide a value for the errorwithError(whatever) { myOption.bind() }
Marc
10/25/2023, 7:37 AMq.value.bind { DataNotFound }
withError(
transform = identity,
block = {
q.value.bind()
})
because bind cant be esolved
q.value
is an Option
Alejandro Serrano.Mena
10/25/2023, 7:48 AMMarc
10/25/2023, 7:51 AMsimon.vergauwen
10/27/2023, 8:00 AMq.value.bind { DataNotFound }
This can be q.value.getOrElse { raise(DataNotFound) }
.withError(
transform = identity,
block = {
q.value.bind()
})
@Alejandro Serrano.Mena we need context receivers for this. Currently Option::bind
is defined on OptionRaise
instead of Raise<None>
.