Document pattern matching alias syntax

Take Belt.Result.flatMap as an example. A simple implementation would be

let flatMap = (this: result<'a, 'err>, f): result<'b, 'err> => {
    switch this {
    | Ok(a) => f(a)
    | Error(_) as err => err

I only discovered this syntax by searching through OCaml syntax. It is undocumented (at least not in Pattern Matching / Destructuring page)

flatMapU at rescript-compiler source has:

let flatMapU opt f = match opt with
  | Ok x -> (f x [@bs])
  | Error y -> Error y

which simply destructures and re-wraps the Error. While it works in this simple use case, I don’t think it is convenient when the number of constructor arguments increases


To add to this, there are a number of OCaml features that are available in ReScript that AFAIK aren’t documented in the ReScript documentation. I think it would be beneficial to include some documentation on them regardless of whether they’re discouraged. Some that come to mind:

  • type parameter constraints
  • universally qualified types
  • type parameter variance
  • let () =
  • Pervasives

All in all, though, I think the quality of documentation has improved tremendously since before the ReScript split and are continuing to rapidly improve. :slight_smile:


Yeah, there’s still a few things that need to be added carefully (it shouldn’t invite users to start code golfing). Can you check the issue tracker and add issues for each topic? I know I added a few, e.g. subtyping / type coercion, but those are probably the hardest to write about (while maintaining our simple and short writing style).

1 Like