gleam icon indicating copy to clipboard operation
gleam copied to clipboard

Compiler: `[..]` breaks Erlang

Open tynanbe opened this issue 1 year ago • 3 comments

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.

tynanbe avatar Mar 09 '24 04:03 tynanbe

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.

wmstack avatar Mar 11 '24 18:03 wmstack

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/

tynanbe avatar Mar 11 '24 19:03 tynanbe

That's right, we only have syntax for fast operations as we don't want you to write slow code.

lpil avatar Mar 12 '24 12:03 lpil