Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't use self-invoking anonymous functions when generating blocks on JavaScript #3968

Open
GearsDatapacks opened this issue Dec 8, 2024 · 1 comment
Labels
help wanted Contributions encouraged priority:medium

Comments

@GearsDatapacks
Copy link
Member

Currently, this gleam code:

pub fn main() {
  let a = {
    let b= 1
    b + 1
  }
}

Compiles to this javascript:

export function main() {
  let a = (() => {
    let b = 1;
    return b + 1;
  })();
  return a;
}

This isn't ideal as it creates an anonymous function which is immediately invoked, which makes the code harder to read and adds the overhead of allocating a function, calling it, then deallocating it. It would be nice to not have to do this.

Perhaps Louis has a particular alternative in mind, but we could generate something like:

let _block;
{
  let b = 1;
  _block = b + 1;
}
a = _block;
return a;

(Or in this case assign directly to a instead of _block, but I'm not sure the compiler would be able to do that)

@lpil
Copy link
Member

lpil commented Dec 8, 2024

Yes!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Contributions encouraged priority:medium
Projects
None yet
Development

No branches or pull requests

2 participants