我想将接收类Receiver上的Kotlin扩展函数与arrow-kt的理解结合起来。在常规Kotlin扩展函数中,this绑定到接收对象;但是,任意理解的EitherEffect阴影接收器this。
suspend fun Receiver.myFun(param: String): Either<Throwable, String> = either {
this.someMethod(...).bind() // Cannot access Receiver.someMethod, <this> is bound to EitherEffect
...
}如何在箭头的理解块(或任何其他一元理解块)中访问接收方上下文?
发布于 2022-03-15 08:30:18
这是从Kotlin继承的一个问题,但是您始终可以通过名称引用外部作用域this来访问它。在这里,您可以通过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
...
}但是,您应该可以在这里简单地调用someMethod,而无需引用this。
suspend fun Receiver.myFun(param: String): Either<Throwable, String> = either {
someMethod(...).bind()
...
}希望这能解决你的问题。
https://stackoverflow.com/questions/71478392
复制相似问题