Accessing array items by index, brackets return option?

I switched to V11 with Core and a bunch of my array access code is now broken. I didn’t see this on the list of breaking changes. My old code had a bunch of items[0] code that would return a specific item (or crash) if the item didn’t exist. Now it returns an option.

Is this something new in V11?
Is there a more compact way of unsafely accessing an array item than Array.getUnsafe like before?
The docs Array & List | ReScript Language Manual seem outdated.

Rescript core causes it.
Do you open it automatically?

If it’s not in the scope the array access by using brackets will return the original data type

The comment with the return value is out of date. Working on a PR to get that fixed and make it clear that accessing an item in an array returns an option.

3 Likes

As others said, having Core open causes this behavior. But if you really want unsafe access, you can write an Array module that has its own get function that returns 'a instead of option<'a>.

// with Core open

let xs = [1, 2, 3]

let m = xs[1] 
//  ^ option<int>

module Array = {
  @get_index external get: (array<'a>, int) => 'a = ""
}

let n = xs[1]
//  ^ int

Playground URL