Hi hivemind. I have an optimisation question. I ha...
# arrow
a
Hi hivemind. I have an optimisation question. I have a
fun mainFun(): Either<A, B>
where A is a sealed interface. I then call this within another function
fun caller(): Either<A, B?>
whose goal is to check if the called
mainFun
returns left and if so, depending on the type of the left, return B? or A. It always return B if
mainFun
returns B. Currently I’m using fold like the below, so I’m wondering if there’s a better way to do that?
Copy code
return mainFun().fold({if(it == SpecialSealedImpl} null.right else it.left) { it.right }
s
You can do this with the
recover
function.
Copy code
mainFun().recover { if(it == SpecialSealedImpl} null else raise(it) }
So you return
null
or
raise
a different error, in this case the same one.
a
Nice!
Thank you
s
My pleasure ☺️