gleam
gleam copied to clipboard
Compiler: `[..]` breaks Erlang
This Gleam
fn a() {
case [] {
[..] -> 1
_ -> 2
}
}
Produces this Erlang in Gleam v1.0.0
-spec a() -> integer().
a() ->
case [] of
[ | _] ->
1;
_ ->
2
end.
Which results in
syntax error before: '|'
% 20| [ | _] ->
% | ^
Note that the formatter currently converts [..] into _, sidestepping the issue.
No such problem was observed with the JS target.
Also, ideally, pattern matching a list should allow an indeterminate number of elements at the start, end or in the middle.
let seq = [1, 2, 3]
case seq {
[x, ..rest] -> // 1, [2, 3]
[..start, y] -> // [1, 2], 3
[x, ..mid, z] -> // 1, [2], 3
}
I believe that only allowing the first pattern is counter-intuitive when all of them are unambiguous.
Also, ideally, pattern matching a list should allow an indeterminate number of elements at the start, end or in the middle. [...] I believe that only allowing the first pattern is counter-intuitive when all of them are unambiguous.
That would be nice; however, those patterns explicitly aren't supported because Erlang doesn't support them. They're not efficient with Gleam's singly linked list structure.
See: https://tour.gleam.run/basics/lists/
That's right, we only have syntax for fast operations as we don't want you to write slow code.