# Platform-independent way to check for array overrun

**URL:** <https://discuss.kotlinlang.org/t/platform-independent-way-to-check-for-array-overrun/19640>\
**Category:** Multiplatform\
**Created:** [October 24, 2020, 7:00pm UTC](https://discuss.kotlinlang.org/t/platform-independent-way-to-check-for-array-overrun/19640 "2020-10-24T19:00:22Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Loke](https://avatars.discourse-cdn.com/v4/letter/l/c68b51/32.png) [@Loke](https://discuss.kotlinlang.org/u/Loke)\
**Post date:** [October 24, 2020, 7:00pm UTC](https://discuss.kotlinlang.org/t/platform-independent-way-to-check-for-array-overrun/19640/1 "2020-10-24T19:00:22Z")

</div>

In my multiplatform application I have an inline class that looks something like this:

```auto
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:

```auto
@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:

```auto
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?
