I have a few specialized classes that I would like to create using Kotlin and Arrow and they will wrap around an Arrow Either monad. I've created the following code to use Kotlin's delegation, but I am wondering whether it can be simplified or made more idiomatic. Any suggestions would be appreciated.
Thank you for your time and interest.
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()}")
}
So, I think that I have a solution to my query. The following code wraps an Either within a new type: Exceptional. This code is the starting point for what I want to build. Eventually, I will have something like the following:
I can pass an instance of this type throughout my backend service and return a more precise exception, error, or value.
A Run: