Recent

Author Topic: Feature announcement: Function References and Anonymous Functions  (Read 91935 times)

PascalDragon

  • Hero Member
  • *****
  • Posts: 6393
  • Compiler Developer
Re: Feature announcement: Function References and Anonymous Functions
« Reply #135 on: February 05, 2026, 09:29:34 pm »
Most people won't even notice any bugs.
That alone is an indication of a significant problem.

I cannot even port the Windows API definitions without running into FPC bugs and deficiencies.    IOW, it doesn't even take writing code to run into problems, just declarations/definitions does it.

How about that for a "new feature" ?... being able to faithfully and correctly port the Windows API from C to FPC.  Personally, I think that would be a great "new feature" (and a rather useful one too.)

But that is not a new problem in 3.3.1.

wpostma

  • New Member
  • *
  • Posts: 13
Re: Feature announcement: Function References and Anonymous Functions
« Reply #136 on: April 21, 2026, 03:25:35 am »
I have built the head of the main branch on FPC, are there unit tests in the FPC repo, for this new reference to keyword and delphi-compat language feature?

Thaddy

  • Hero Member
  • *****
  • Posts: 19129
  • Glad to be alive.
Re: Feature announcement: Function References and Anonymous Functions
« Reply #137 on: April 21, 2026, 12:41:10 pm »
a) in Delphi mode they are already automatically activated in current trunk, in Objfpc mode you have to add the modeswitches manually.
b) yes, many, see the release notes for the features in https://wiki.freepascal.org/FPC_New_Features_Trunk#Support_for_Function_References_and_Anonymous_Functions and follow the commit. I don't remember the particular tests from memory.

Both features are Delphi compatible. (near, but not 100%, explained in the feature announcement)

There are also some examples in the wiki, like the third example in the smartpointers subject (which I wrote) but there are more. Also many examples on the forum.
Simple example:
Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2. uses sysutils,classes;
  3. var
  4.   List:TStringlist;
  5.   i:integer;
  6. begin
  7.   List := TStringlist.Create;
  8.   AddExitProc(procedure begin list.free;end);// anonymous
  9.   for i := 0 to 9 do List.Add(i.ToString);
  10.   writeln(list.text);
  11.   readln;
  12. end.
 
In general, in mode Delphi, almost everything, if not all, works for both features, but read the announcement.

All examples from Barry Kelly - former Delphi compiler engineer - work for example. As do all of the examples regarding the two features from the public repository from Embarcadero.
« Last Edit: April 21, 2026, 01:20:47 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12842
  • FPC developer.
Re: Feature announcement: Function References and Anonymous Functions
« Reply #138 on: April 21, 2026, 01:24:25 pm »
And at least the basic support has been added to TThread, like queue() has the anonymous method overhead.

Thaddy

  • Hero Member
  • *****
  • Posts: 19129
  • Glad to be alive.
Re: Feature announcement: Function References and Anonymous Functions
« Reply #139 on: April 21, 2026, 01:47:33 pm »
@Marcov

Well, there is some overhead, but writing things like:
Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2. uses sysutils, classes;
  3. var
  4.   T:TThread;
  5. begin
  6.   T := TThread.ExecuteInThread(procedure //some long running code
  7.                                begin
  8.                                  writeln('Thread start...');
  9.                                  // load simulation
  10.                                  Sleep(5000);
  11.                                  writeln('Thread finished');
  12.                                end);
  13.   T.Waitfor;// don't leave the waits out...
  14.   writeln('Done');
  15.   readln;
  16. end.
is great. The implicit COM interface setup code has no noticeable effect on the actual running code except in high performance threaded applications.

More elaborate, with good practice, here:
https://forum.lazarus.freepascal.org/index.php/topic,72079.msg563706.html#msg563706

Demonstrates that even with anon procedures the code is considerably faster  than the sequential approach. (The subject of that forum thread does not matter)
« Last Edit: April 21, 2026, 02:11:28 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

PascalDragon

  • Hero Member
  • *****
  • Posts: 6393
  • Compiler Developer
Re: Feature announcement: Function References and Anonymous Functions
« Reply #140 on: April 23, 2026, 10:04:02 pm »
I have built the head of the main branch on FPC, are there unit tests in the FPC repo, for this new reference to keyword and delphi-compat language feature?

The tests are tests/test/tfuncref*.pp and tests/test/tanonfunc*.pp.

lebao3105

  • New Member
  • *
  • Posts: 20
Re: Feature announcement: Function References and Anonymous Functions
« Reply #141 on: May 06, 2026, 03:04:09 pm »
I have the following piece of code:

Code: Pascal  [Select][+][-]
  1. program test;
  2. {$modeswitch functionreferences}
  3. {$modeswitch anonymousfunctions}
  4.  
  5. type
  6.     generic TTestFunction<T> = reference to function(arg: string): boolean;
  7.  
  8. generic Procedure Testit<T>(callback: specialize TTestFunction<T>);
  9. begin
  10.     callback('Hello world from TestIt!');
  11. end;
  12.  
  13. Begin
  14.     specialize TestIt<string>(
  15.         function (arg: string): boolean
  16.         begin
  17.             writeln(arg);
  18.             exit (true);
  19.         end
  20.     );
  21. End.
  22.  

FPC's complaination:
Code: Pascal  [Select][+][-]
  1. function (arg: string): boolean
  2.                       ^ indentier expected but ":" found
  3.  

However the code compiles with ObjFPC / Delphi mode used. Like...why?

Update: Found the reason. I checked my usages of the same function type, and yes, {$modeswitch result} once again helped me.
« Last Edit: May 06, 2026, 04:28:57 pm by lebao3105 »
My GitHub+GitLab username: lebao3105.

Thaddy

  • Hero Member
  • *****
  • Posts: 19129
  • Glad to be alive.
Re: Feature announcement: Function References and Anonymous Functions
« Reply #142 on: May 07, 2026, 04:19:10 pm »
The other modes are not using Classes. You need to choose a mode that supports classes.
objects are fine constructs. You can even initialize them with constructors.

PascalDragon

  • Hero Member
  • *****
  • Posts: 6393
  • Compiler Developer
Re: Feature announcement: Function References and Anonymous Functions
« Reply #143 on: May 12, 2026, 08:57:47 pm »
Update: Found the reason. I checked my usages of the same function type, and yes, {$modeswitch result} once again helped me.

For modes without $ModeSwitch Result enabled you need to add a name for your result variable just like for operator overloads in those modes. So your code would become this:

Code: Pascal  [Select][+][-]
  1.    specialize TestIt<string>(
  2.         function (arg: string) res : boolean
  3.         begin
  4.             writeln(arg);
  5.             exit (true);
  6.         end
  7.     );

 

TinyPortal © 2005-2018