Destructuring example for lists does not work

In the documentation there is this example for destructuring a list

// List
let myList = list{1, 2, 3}
let list{head, ...tail} = myList // 1 assigned to `head`, `list{2, 3}` assigned to tail

However in the Rescript playground (and also when trying to build) it produces this error:
File "lam_coercion.ml", line 218, characters 4-10: Assertion failed

Not sure if this is an “issue” or if the documentation needs to be updated.

looks like a compiler bug.

opened an issue

Underlying issue is: If the list was empty, getting it’s head would raise exception.

Correct behavior for compiler would be to show warning about missing case instead of failed compilation, I think.

In practice list destructuring is done with switch to handle empty list case.

1 Like

Note that this bug only seems to occur on the top-level. If you wrap it in a function, it compiles correctly.

// List
let myList = list{1, 2, 3}
let f = () => {
  let list{head, ...tail} = myList // 1 assigned to `head`, `list{2, 3}` assigned to tail
}

Looks like the same issue as the one from 2018: https://github.com/rescript-lang/rescript-compiler/issues/2474