0
[FIXED] How to fix smart cast to 'Stack.Node' is impossible in this stack code?
https://code.sololearn.com/cQwl2PvA4JRp/?ref=app So, I tried to make a stack in kotlin. But I don't know how to fix that "smart cast" thing, especially in pop function? What should I do?
3 ответов
+ 1
It is complaining because it's possible that the mutable value of head could possibly be changed by another thread.
You basically already fixed it by copying it to node. Then you just needed to use that local copy that no other threads could possibly access in place of head.
public fun pop() {
var node = head
if (node != null) {
head = node.next
node = null
}
}
Try that it should work
https://stackoverflow.com/questions/46701042/kotlin-smart-cast-is-impossible-because-the-property-could-have-been-changed-b
+ 1
Great, that works. Thank you ChaoticDawg
+ 1
Np, it was a good question. I learned while answering it so that is always an added bonus!