mcandre
mcandre

Reputation: 24642

How can I use anonymous methods in Free Pascal?

I tried to use Delphi's syntax for anonymous methods:

type
    fun = reference to function(): Integer;

Fpc shows a syntax error:

Error: Identifier not found "reference"

What's the Free Pascal equivalent to Delphi's anonymous methods, if any?

Upvotes: 7

Views: 5188

Answers (2)

cpicanco
cpicanco

Reputation: 294

Anonymous methods are supported. Useful references:

The GitLab issue: https://gitlab.com/freepascal.org/fpc/source/-/issues/24481

The forum annoucement: https://forum.lazarus.freepascal.org/index.php?topic=59468.0

Finally, some examples given by Sven in the annoucement:

type
  TFunc = function: LongInt;
 
var
  p: TProcedure;
  f: TFunc;
  n: TNotifyEvent;
begin
  procedure(const aArg: String)
  begin
    Writeln(aArg);
  end('Hello World');
 
  p := procedure
       begin
             Writeln('Foobar');
           end;
  p();
 
  n := procedure(aSender: TObject);
       begin
             Writeln(HexStr(Pointer(aSender));
           end;
  n(Nil);
 
  f := function MyRes : LongInt;
       begin
             MyRes := 42;
           end;
  Writeln(f());
end.

Upvotes: 1

David Heffernan
David Heffernan

Reputation: 613382

Anonymous methods are not implemented in FreePascal. The list of such features is here.

Upvotes: 7

Related Questions