Should we limit smart casting? #7323
Replies: 3 comments
-
That happens because |
Beta Was this translation helpful? Give feedback.
-
@danieldaeschle I think it's a serious limitation in Kotlin, and it's good that we don't have it. We do not allow |
Beta Was this translation helpful? Give feedback.
-
It's not a limitation of kotlin, it's because mutable smart casting can violate memory safety. This works: val t = Test(Test("asdsad"))
if (t.x is Test) {
val a = t.x.x
println(a)
} |
Beta Was this translation helpful? Give feedback.
-
Kotlin has a limitation as well: https://play.kotlinlang.org/#eyJ2ZXJzaW9uIjoiMS40LjIwIiwicGxhdGZvcm0iOiJqYXZhIiwiYXJncyI6IiIsImpzQ29kZSI6IiIsIm5vbmVNYXJrZXJzIjp0cnVlLCJ0aGVtZSI6ImlkZWEiLCJjb2RlIjoiLyoqXG4gKiBZb3UgY2FuIGVkaXQsIHJ1biwgYW5kIHNoYXJlIHRoaXMgY29kZS4gXG4gKiBwbGF5LmtvdGxpbmxhbmcub3JnIFxuICovXG5cbmRhdGEgY2xhc3MgVGVzdCAodmFsIHg6IEFueSlcblxuZnVuIG1haW4oKSB7XG4gICAgdmFyIHQgPSBUZXN0KFRlc3QoXCJhc2RzYWRcIikpXG4gICAgaWYgKHQueCBpcyBUZXN0KSB7XG4gICAgICAgIHZhbCBhID0gdC54LnhcbiAgICB9XG59In0=
Just encountered this and want to share.
Beta Was this translation helpful? Give feedback.
All reactions