any way to transform this into a `tailrec`? And ge...
# announcements
e
any way to transform this into a
tailrec
? And get rid of the
!!
?
Copy code
while (window.flags has Wf.ChildWindow && window.parentWindow != null)
    window = window.parentWindow!!
s
as far as tailrec goes, something like this probably
Copy code
tailrec fun findWindow(window: Window): Window {
  return if (window.flags has Wf.ChildWindow && window.parentWindow != null) {
    findWindow(window.parentWindow)
  } else {
    window
  }
}
e
window.parentWindow
is
Window?
, that's the problem
s
also if I had to guess the problem you’re seeing with smart casting is probably because
window.parentWindow
is a
var
or a
val
with a computed getter
e
var
indeed
s
you’re probably just going to have to cache in a local
val
e
uh, yeah
s
or, well, bleh, it’s probably actually the combined if
e
this appears to trigger no warning from the compiler
Copy code
tailrec fun findWindow(window: Window): Window {
                val parent = window.parentWindow
                return if (window.flags has Wf.ChildWindow && parent != null) findWindow(parent) else window
            }
but
findWindow
isnt the last call.. or is it?
s
compiler doesn’t complain
e
no
s
tailrec
doesn’t mean that the recursive call has to literally be the last visible line in a function
there just can’t be logic after the function call. (and the other branch needs to be terminal)
you could invert that
if
to make it look more tail recursive, but it’s really the same thing
Copy code
tailrec fun findWindow(window: Window): Window {
  val parentWindow = window.parentWindow
  return if (Wf.ChildWindow !in window.flags || parentWindow == null) {
    window
  } else {
    findWindow(parentWindow)
  }
}
e
got it, thanks