我有一些专门的类,我想使用 Kotlin 和 Arrow 创建它们,它们将环绕 Arrow Either monad。我创建了以下代码来使用 Kotlin 的委托,但我想知道它是否可以简化或变得更惯用。任何建议,将不胜感激。
感谢您的时间与兴趣。
internal data class EitherWrapper<L,R>(private var delegate: Either<L,R>) {
internal operator fun getValue(thisRef: Any?, property: KProperty<*>): Either<L,R> {
return delegate
}
internal operator fun setValue(thisRef: Any?, property: KProperty<*>, value: Either<L,R>) {
delegate = value
}
}
fun main(args: Array<String>) {
val delegate: Either<Exception, String> = Right("howdy")
val myeither: Either<Exception, String> by EitherWrapper(delegate)
println("IsLeft(): ${myeither.isLeft()}")
println("IsRight(): ${myeither.isRight()}")
}