There is a fourth option in addition to the ones in mfulton26's answer.
By using the ?.
operator it is possible to call methods as well as fields without dealing with let
or using local variables.
Some code for context:
var factory: ServerSocketFactory = SSLServerSocketFactory.getDefault();
socket = factory.createServerSocket(port)
socket.close()//smartcast impossible
socket?.close()//Smartcast possible. And works when called
It works with methods, fields and all the other things I tried to get it to work.
So in order to solve the issue, instead of having to use manual casts or using local variables, you can use ?.
to call the methods.
For reference, this was tested in Kotlin 1.1.4-3
, but also tested in 1.1.51
and 1.1.60
. There's no guarantee it works on other versions, it could be a new feature.
Using the ?.
operator can't be used in your case since it's a passed variable that's the problem. The Elvis operator can be used as an alternative, and it's probably the one that requires the least amount of code. Instead of using continue
though, return
could also be used.
Using manual casting could also be an option, but this isn't null safe:
queue.add(left as Node);
Meaning if left has changed on a different thread, the program will crash.
n.left?.let { queue.add(it) }
I think? – Jorn Vernee