In my multiplatform application I have an inline class that looks something like this:
inline class Foo(val content: IntArray) {
operator fun get(i: Int) = content[i]
}
In my unit tests I have a test that ensures that an overrun will fail:
@Test
fun testIncorrectIndex() {
val foo = ... // Get an instance of Foo here. The array is 10 elements long.
assertFails {
foo[10]
}
}
I have just implemented support for Javascript, and now I have a problem. In JS, the array dereferencing does not throw an exception when the index is outside the allowable values.
What I did was to add an extra check, like so:
assertFails {
foo[10] as Int
}
The explicit type cast will trigger an error if the dereferencing returned undefined
. However, the compiler warns me that this is a useless cast and will be very insistent that I remove it.
Is there a proper way to handle this case?