简体   繁体   中英

Access a shadowed receiver

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

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 {
  this@myFun.someMethod(...).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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM