Array access is sometimes option

Hi, when I access an array with arr[0] sometimes it returns the element, and sometimes it returns a type option on the element, and I have to call Option.getExn(), why?

Hi @fccm I think you might have opened Belt

module WithBelt = {
  open Belt
  let a = [1, 2, 3]

  //type of b is option<int>
  let b = a[0]
}

module WithoutBelt = {
  let a = [1, 2, 3]

  //type of b is int
  let b = a[0]
}

PlayGround

2 Likes

Indeed, I didn’t notice this trick :slight_smile:
Thanks!

Here’s a further explanation in the docs about this behavior.