[Pas2js] Anonymous functions

Michael Van Canneyt michael at freepascal.org
Wed Dec 12 14:25:20 CET 2018



Congratulations, Mattias!

this will surely be greeted by many users with great enthousiasm !

Michael.


On Wed, 12 Dec 2018, Mattias Gaertner via Pas2js wrote:

> 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
> _______________________________________________
> Pas2js maillist  -  Pas2js at lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/pas2js


More information about the Pas2js mailing list