Is there any know issue related to evaluation orde...
# eap
t
Is there any know issue related to evaluation order of delegated properties and other object's fields initialization expressions? I get "undefined" related errors when using properties that are delegated inside other field initialization expressions. Edit: Turned the issue was caused by completely different bug. Compare the result of:
Copy code
fun main() {
    println(arrayOf("x")[1])
}
on JVM and JS - https://pl.kotl.in/OQW7p5XAL I guess that's stdlib bug.
i
The behavior or Array.get when index is out of array bounds is platform-dependent: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-array/get.html
t
@ilya.gorbunov Still... it violates method's signature by returning value of different type(undefined).
i
Yes, some bad things can happen in JS. The lesson here is that it's not a good idea to index arrays out of their bounds.
t
I believe this should be changed and behaviour should be consistent with JVM. When for performance reasons we want native JS array behaviour, the signature of get should be different (
get(index :  Int) : T*?*
)
I mean... without any hacks we're getting value that doesn't satisfies expression type, which is... very bad. Kotlin is strongly typed, isn't it?
i
This is not the first case where values can be not satisfying the declared types in Kotlin: other examples are unchecked casts, leaking uninitialized instances. Kotlin is strongly typed to some practical degree.
t
Unchecked casts are checked at runtime. For leaking uninitialized instance... eh, that's sad, but difficult to fix at this point. But the arrays are actually easy to fix, but I agree now, that do not introduce unique flaw to the language.
f
Unchecked casts of type arguments are not checked at runtime. E.g.
Copy code
fun foo(x : List<Int>) {
    x as List<String> // Dangerous
}
t
Ah, that's due to type erasure. But when you retrieve value of type specified by type parameter, than a type check occurs.