Open Belt; and array access syntax sugar


This is probably a super minor concern compared to everything else out there (and it’s easily worked around), but I feel like it’s worth bringing up.

Basically, if you do open Belt, the array access syntax sugar (e.g. matrix[i][j]) ends up breaking, as Belt.Array.get returns an option('a) instead of 'a, and Belt.Array.set returns a bool instead of a unit.

I’ve worked around this by including an ArrayOverlay module (per @thangngoc89’s suggestion) that replaces these with the original stdlib implementations, but this seems kinda weird. Other stdlibs like Core also retain the original Array.get/Array.set signatures (presumably to support this syntax sugar), so I was wondering if Belt should do the same.


This is a case where Belt opts for more safety than the stdlib, and frankly I appreciate it :slight_smile: it means that get & set will never raise


You can read about error aware return types in “Real World OCaml”.

The best way in OCaml to signal an error is to include that error in your return value. Including errors in the return values of your functions requires the caller to handle the error explicitly, allowing the caller to make the choice of whether to recover from the error or propagate it onward.