https://kotlinlang.org logo
#arrow
Title
# arrow
m

Marc

10/25/2023, 7:19 AM
I do have this:
Copy code
class 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.vergauwen
a

Alejandro Serrano.Mena

10/25/2023, 7:20 AM
you should be able to use
recover
for this
m

Marc

10/25/2023, 7:21 AM
could you elavorate a bit, please? 🙏🏻
a

Alejandro Serrano.Mena

10/25/2023, 7:29 AM
I don't know exactly what's happening there, but here's a go:
Copy code
override 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)
  }
}
m

Marc

10/25/2023, 7:35 AM
ooook, that makes sense.
and looks great
btw did
Option
lost
bind
in Arrow 1.2.0?
a

Alejandro Serrano.Mena

10/25/2023, 7:36 AM
it didn't lose
bind
everywhere, only in those places in which you were to provide a value for the error
this is because you can now say
withError(whatever) { myOption.bind() }
🙏🏻 1
m

Marc

10/25/2023, 7:37 AM
how should i replace it then?
Copy code
q.value.bind { DataNotFound }
nice
hmm, IDE not able to resolve the bind on option
is that ok?
Copy code
withError(
    transform = identity,
    block = {
        q.value.bind()
    })
because bind cant be esolved
q.value
is an
Option
a

Alejandro Serrano.Mena

10/25/2023, 7:48 AM
mmm, I'll have to check...
🙏🏻 1
m

Marc

10/25/2023, 7:51 AM
I used fold lately…
s

simon.vergauwen

10/27/2023, 8:00 AM
Copy code
q.value.bind { DataNotFound }
This can be
q.value.getOrElse { raise(DataNotFound) }
.
Copy code
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>
.
2 Views