Option type required after open Belt

Hello, I am a little confused with what I see (using the playground).

let x = ["a", "b", "c"]
let y = x[2] ++ " hi"

That compiles without error. After adding open Belt:

open Belt
let x = ["a", "b", "c"]
let y = x[2] ++ " hi"

I get the error:

[E] Line 3, column 8:
This has type: option<string>
Somewhere wanted: string

What changes?

1 Like

Accessing index of an array with Belt library returns an optional.

The Belt library is already there in the first example. What does “open Belt” do that it changes the behaviour of indexed access?

It brings another definition of Array.get into scope. x[2] is just syntax sugar for Array.get(x, 2).

3 Likes