Don't know if it's me doing something wrong, but I...
# arrow
s
Don't know if it's me doing something wrong, but I got an
Either
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:
Copy code
@Deprecated(
    RedundantAPI + "Use isLeft()",
    ReplaceWith("isLeft()")
  )
  @JsName("_isLeft")
  internal abstract val isLeft: Boolean
With the
Copy code
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?
Screenshots for illustration 😄 I will just go with a nice and comfortable
onLeft
for now, but I was just super curious why the "wrong" one is picked up here
a
unfortunately smart casting doesn't work very well when type variables are involved 😞
s
But, if the deprecated field didn't exist, wouldn't it just work here? And I wonder, is the isLeft() function basically impossible to call? At least if the deprecated one doesn't get deleted?
a
the problem is not that
isLeft()
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.
s
So if the contract doesn't work, is it there in hopes that in a future version it will work, or could it just be removed if it doesn't do what it should in this case?
a
the contract doesn't work, we hope in the future (and maybe this future is already 2.0) it will work as expected
🤞 1