Access a shadowed receiver

167 Views Asked by At

I would like to combine a Kotlin extension function on some receiver class Receiver with arrow-kt's either comprehension. In a regular Kotlin extension function, this binds to the receiver object; however, the either-comprehension EitherEffect shadows the Receiver this:

suspend fun Receiver.myFun(param: String): Either<Throwable, String> = either {
  this.someMethod(...).bind() // Cannot access Receiver.someMethod, <this> is bound to EitherEffect
  ...
}

How can I access the receiver context within arrow's either-comprehension block (or any other monadic comprehension block)?

1

There are 1 best solutions below

0
On BEST ANSWER

This is an issue inherited from Kotlin, but you can always access outer scoped this by referencing it by name. Here you can access Receiver by referencing it by this@myFun.

suspend fun Receiver.myFun(param: String): Either<Throwable, String> = either {
  [email protected](...).bind() // Cannot access Receiver.someMethod, <this> is bound to EitherEffect
  ...
}

However, you should be able to simply call someMethod here without referencing this.

suspend fun Receiver.myFun(param: String): Either<Throwable, String> = either {
  someMethod(...).bind()
  ...
}

Hope that solves your issue.