Stylianos Gakis
02/08/2024, 9:30 PMEither
and I want to do isLeft()
on it and I expect then inside the lambda that the variable would be smart-cast to the left type, but it's just not doing so for me.
Jumping to the definition of the isLeft()
it takes me to this:
@Deprecated(
RedundantAPI + "Use isLeft()",
ReplaceWith("isLeft()")
)
@JsName("_isLeft")
internal abstract val isLeft: Boolean
With the
public fun isLeft(): Boolean {
contract {
returns(true) implies (this@Either is Left<A>)
returns(false) implies (this@Either is Right<B>)
}
return this@Either is Left<A>
}
Being right there below it, very pretty, non deprecated and with the contract along with it. But it looks like it auto-picks the wrong one and I don't get any of those goodies 😰
Is this a known problem, or is there a way for me to somehow force it to use the function instead of the abstract val which it automatically picks somehow?Stylianos Gakis
02/08/2024, 9:41 PMonLeft
for now, but I was just super curious why the "wrong" one is picked up hereAlejandro Serrano.Mena
02/09/2024, 9:02 AMStylianos Gakis
02/09/2024, 9:44 AMAlejandro Serrano.Mena
02/09/2024, 9:56 AMisLeft()
cannot be called (it would return the corresponding expected Boolean), but rather that the contract in isLeft
is not really taken into consideration by the compiler, so it doesn't know that you have a Either.Left
if it returns true. This is different from the is
statement, where the compiler can readily see it.Stylianos Gakis
02/09/2024, 1:33 PMAlejandro Serrano.Mena
02/09/2024, 2:29 PM