How to create an alias to bind method in Arrow-kt?

289 Views Asked by At

In Arrow-kt I'd like to create an alias to the bind() - for a kind of custom lib to use Arrow.

I'd expect the following to work but it doesn't:

suspend fun <F, S> Either<F, S>.bindMy(): S = this.bind()

The method I wanna target is

public interface EffectScope<in R> {
  ...
  public suspend fun <B> Either<R, B>.bind(): B

I guess it doesn't work as I expect because of the EffectScope.

Any idea how I could make it work? Thx

1

There are 1 best solutions below

0
On

bind is defined in EffectScope or Raise (Arrow 2.0 snapshot) as an extension method over Either<A, B>.

You can go about this in different ways.

  1. Use the upcoming context receivers feature if you are in JVM.
context(EffectScope<E>)
fun <E, A> Either<E, A>.myBind(): A = fold({ shift(it) }, ::identity)
  1. Extend the EffectScope interface and define your fold machinery as Arrow does for EffectScope and Effect. Unfortunately, until context receivers are available, this is a more heavyweight solution. If you use the 2.0 snapshot, where all this is simpler, you will need to provide similar machinery like Raise and Effect.

If you'd like some help with any of this, we hang out in the Kotlin slack #Arrow channel