gleam icon indicating copy to clipboard operation
gleam copied to clipboard

Compiler: At least one form of `use` misses a semicolon when compiled to JS

Open tynanbe opened this issue 11 months ago • 2 comments

This Gleam

fn b() {
  {
    use a <- fn(cb) { cb(1) }
    a
  }
  {
    use b <- fn(cb) { cb(2) }
    b
  }
  3
}

Produces this JavaScript with Gleam v1.0.0

function b() {
  ((cb) => { return cb(1); })((a) => { return a; })
  ((cb) => { return cb(2); })((b) => { return b; })
  return 3;
}

Causing this error

  ((cb) => { return cb(2); })((b) => { return b; })
  ^

TypeError: (intermediate value)(...) is not a function

Due to a missing semicolon.

tynanbe avatar Mar 09 '24 04:03 tynanbe

Thank you

lpil avatar Mar 13 '24 12:03 lpil

FWIW as far as I could tell this actually is not specific to use but rather to any block that returns a function

fn b() {
  {
    fn(cb) { cb(1) }
  }
  {
    fn(cb) { cb(2) }
  }
  3
}

This also fails with TypeError: cb is not a function because it compiles to

function b() {
  ((cb) => { return cb(1); })
  ((cb) => { return cb(2); })
  return 3;
}

Ultimate root cause being that blocks cause the function definition to be parenthesized and thus callable

Acepie avatar Mar 14 '24 21:03 Acepie