[JSCodeGen] "LET" keyword

Issue #194 new
Toky Olivier Razanakotonarivo created an issue

Hello, can you implement please “LET” keyword? at least for JSCodeGen?

As you can see here: https://stackoverflow.com/questions/11488014/asynchronous-process-inside-a-javascript-for-loop

VAR doesn’t work in an event loop of the form

for var i:=0 to 5 do begin
  asynchrounousTask(procedure () 
  begin
    print(i); //it will be written "6" always.
  end);
end;

Is it possible to add also “forEach” helper with arrays?

var arr: array of int;
arr.forEach(procedure(i: int)
begin
  print (i);
end);

Thank you Eric.

Comments (5)

  1. Eric Grange repo owner

    As an alternative, the codegen could be adapted to use “let” instead of “var” for loop variables. This would diverge from Delphi’s capture mechanism, but be more inline with expectations.

    Generally speaking “let” could be leveraged throughout the codegen to simplify scoping, as JS’s “let” is closer to Pascal’s “var” most of the time, the exception being capture of loop variables.

    caniuse indicates to 96% support for “let”, so it should be safe to migrate the codegen

    Supporting forEach should already be possible through helpers.

  2. Eric Grange repo owner

    Not as straightforward as I first thought, as the loop var is currently declared outside of the “for” expression in the AST, so the codegen will need extra hinting so it won’t declare the variable outside of the loop.

  3. Log in to comment