Or do I just read `result.value` directly?
# arrow
l
Or do I just read
result.value
directly?
w
Yes, once you've ensured that its
Right
(or
Left
), you can access the value of either side simply using the
value
property.
👍 1
l
OK. I was expecting an extraction syntax like in Rust. 🙂 But this works too.
w
You can also avoid the when block and use functions like
onLeft
,
onRight
,
getOrElse
, etc.
l
Good to know. In this case I think the when is most logical, but won’t always be
a
in Kotlin we use mostly "smart casting", which means that once you go into a branch where you are sure that
result
has type
Either.Right
, it behaves as that type in every situation
s
Ah too bad. Custom
returns
&
implies
contracts not supported in Kotlin 2.0. Otherwise this would've safely worked as well.
Copy code
val x: Either<String, Int> = 1.right()
if (x.isRight()) {
    x.value
}