HieiJ
11/15/2021, 9:01 PMval str1 = "hello".toOption().right()
val str2 = "world".toOption().right()
val output: Either<Any, Option<String>> = either {
option {
val value1 = str1.bind().bind()
val value2 = str2.bind().bind()
value1 + value2
}
}
I'm wondering if there is any "generic" way compose in order to obtain (for this example) an eitherOption { }
block where I can call .bind()
just one time. I know that Option and Either nested like this are not so useful, this is just for having a simple example 😃simon.vergauwen
11/15/2021, 9:05 PMcontext(EitherEffect<E>, OptionEffect)
suspend fun <E, A> Either<E, Option<A>.bind(): A = bind().bind()
simon.vergauwen
11/15/2021, 9:07 PMinline fold
underneath the hoodsimon.vergauwen
11/15/2021, 9:08 PMsimon.vergauwen
11/15/2021, 9:09 PMinterface EitherOptionEffect : EitherEffect<E> OptionEffect {
suspend fun <E, A> Either<E, Option<A>.bind(): A
}
Which you can make concrete by doing something likeesimon.vergauwen
11/15/2021, 9:11 PMsuspend <E, A>. optionEither(f: suspend EitherOptionEffect<E>.() -> A): Either<E, Option<A>> {
either<E, Option<A>> {
option<A> {
f(object : EitherOptionEffect<E>,
EitherEffect<E> by this@either,
OptionEffect by this@option { })
}
}
}
HieiJ
11/15/2021, 9:16 PM