`curr.left` is not so complex for me…
# announcements
e
curr.left
is not so complex for me…
j
It’s just saying that it can’t do the smart cast for you. As in: The compiler is not sure if curr.left is really a Node<T>. Should work fine with an explicit cast:
curr = curr.left as Node<T>
e
yep, it works, you are right. Very strange, that compiler can’t do smart cast in such simple case. Anyway, thank you.
g
because compiler cannot guarantee you that this variable doesn’t changes (property getter can be dynamic, reflections)
you can cache it to local variable for smart cast:
Copy code
var curr = root
   val left = curr.left
   while (left != null) {
      curr = left
   }
   return curr
👍 2
a
+1 to local cache. I still don't know if that's best practice, but I often do that in these errors where it can't smart cast because it could change (even though that's highly unlikely in the time it takes to enter the if statement.)
g
imo it’s good practice and improve readability in many cases, much better than nested let for instance. And of course help with smart cast