Home > Blockchain >  Access a shadowed receiver
Access a shadowed receiver

Time:03-16

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)?

CodePudding user response:

schuster,

This is an issue inherit 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.

  • Related