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?

30th Apr 2020, 6:06 AM
M Fauzan Rizky Agusta
M Fauzan Rizky Agusta - avatar
3 odpowiedzi
+ 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
30th Apr 2020, 6:36 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Great, that works. Thank you ChaoticDawg
30th Apr 2020, 6:40 AM
M Fauzan Rizky Agusta
M Fauzan Rizky Agusta - avatar
+ 1
Np, it was a good question. I learned while answering it so that is always an added bonus!
30th Apr 2020, 6:48 AM
ChaoticDawg
ChaoticDawg - avatar