[Pas2js] Anonymous functions

Mattias Gaertner nc-gaertnma at netcologne.de
Wed Dec 12 13:43:42 CET 2018


Hi,

I implemented anonymous functions in trunk pas2js. Now I need some
testers.

Delphi calls them "anonymous methods":
http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/devcommon/anonymousmethods_xml.html

IMO "method" in Pascal usually refer to functions of objects,
so I think "anonymous functions" is the better name.

type
  TRefProc = reference to procedure;
  TProc = procedure;
procedure DoIt(arg: TRefProc);
var 
  ref: TRefProc;
  proc: TProc;
begin
  ref:=procedure begin end; // assign
  ref:=procedure  // note the omitted semicolon
    var i: integer; // var, types, const, local procedures
    begin
    end; 
  DoIt(procedure begin end); // pass as argument
  refproc:=procedure assembler asm // embed JavaScript
      console.log("foo");
    end; 
  // Note that typecasting to non "reference to" does not make 
  // a difference because in JS all functions are closures:
  proc:=TProc(procedure begin end);
end;

Note to Lazarus users: you need trunk Lazarus, as codetools in Lazarus
2.0 and below do not support anonymous functions.

Mattias


More information about the Pas2js mailing list