Recent

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

munair

  • Hero Member
  • *****
  • Posts: 887
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: Feature announcement: Function References and Anonymous Functions
« Reply #120 on: May 22, 2025, 01:26:22 am »
  for I := 0 to function: integer
  begin
    if StrLen < TypeBits then Result := StrLen
    else
      Result := TypeBits;
  end() - 1 do
It made me actually quite curious, but the SharpBASIC compiler supports this construct, because the parser expects expressions, which functions are a part of. I never tried it before. It outputs 0..9:

Code: Text  [Select][+][-]
  1. ' SharpBASIC for-loop with functions
  2. ' ----------------------------------
  3. incl "lib/sys.sbi";
  4.  
  5. decl func f1():int;
  6. decl func f2():int;
  7.  
  8. dim i:itr;
  9.  
  10. main do
  11.   for i = f1() to f2() :++ do
  12.     print(i);
  13.   end
  14.   print();
  15. end
  16.  
  17. func f1():int do f1 = 0; end
  18. func f2():int do f2 = 9; end
  19.  

But it needs a valid function reference. The keyword func or function alone is of course not enough.
« Last Edit: May 22, 2025, 01:47:11 am by munair »
It's only logical.

PascalDragon

  • Hero Member
  • *****
  • Posts: 6311
  • Compiler Developer
Re: Feature announcement: Function References and Anonymous Functions
« Reply #121 on: May 22, 2025, 10:04:04 pm »
;) That won't work.(I hope... :o )
Loop variables need to be known at compile time.

That does work, after all its only calling a function (no matter how bad it looks). The ranges of the loop don't need to be known at compile time (cause otherwise any for i := 0 to SomeList.Count do-loop would not work). The important point is that the ranges are only evaluated once and that is guaranteed by the code that the compiler generates.

fpc4.0  release time ?

Considering that it's not even clear whether the next major release will be 3.4 or 4.0 I'd say 4.0 will be released at least in this century. 🤷‍♀️

Bart

  • Hero Member
  • *****
  • Posts: 5674
    • Bart en Mariska's Webstek
Re: Feature announcement: Function References and Anonymous Functions
« Reply #122 on: May 22, 2025, 10:14:44 pm »
... I'd say 4.0 will be released at least in this century. 🤷‍♀️

Are you sure?

Bart

PascalDragon

  • Hero Member
  • *****
  • Posts: 6311
  • Compiler Developer
Re: Feature announcement: Function References and Anonymous Functions
« Reply #123 on: May 25, 2025, 09:36:25 pm »
... I'd say 4.0 will be released at least in this century. 🤷‍♀️

Are you sure?

I'm an optimist, so I'm at least confident 😅

d2010

  • Sr. Member
  • ****
  • Posts: 251
Re: Feature announcement: Function References and Anonymous Functions
« Reply #124 on: August 24, 2025, 11:28:02 pm »
Dear Free Pascal Community,

The Free Pascal Developer team is pleased to finally announce the addition of a long awaited feature, though to be precise it's two different, but very much related features: Function References and Anonymous Functions. These two features can be used independantly of each other, but their greatest power they unfold when used together.

Why You using  same syntax, perhaps overflow-up too much the tokenizer?
I think you replace "procedure" with "procedure.ref"
      function with funct.ref
You cannot use
You must quote name of procedure  with other chars, diferent  '' ' ' ' ' ''
  F"nop"   double 0quotes ""
The type string of too good, and you

 
Code: [Select]
  p := procedure(begin.ref,Writeln('Foobar'),end)
  p();

Code: Pascal  [Select][+][-]
  1. type
  2.   TFunc = function: LongInt;
  3.  
  4. var
  5.   p: TProcedure;
  6.   f: TFunc;
  7.   n: TNotifyEvent;
  8. begin
  9.   procedure("Nop",const aArg: String)
  10.   begin
  11.     Writeln(aArg);
  12.   end('Hello World');
  13.  
  14.   p := procedure("Nop",Writeln('Foobar');end;
  15.   p();
  16.   n := procedure("Nop",aSender: TObject);
  17.        begin
  18.              Writeln(HexStr(Pointer(aSender));
  19.            end;
  20.   n(Nil);
  21.  
  22.   f := function("MyRes" : LongInt;
  23.        begin
  24.              "MyRes" := 42;
  25.            end;
  26.   Writeln(f());
  27. end.

 :-[
         Old codes , previous here --
Code: Pascal  [Select][+][-]
  1. type
  2.   TFunc = function: LongInt;
  3.  
  4. var
  5.   p: TProcedure;
  6.   f: TFunc;
  7.   n: TNotifyEvent;
  8. begin
  9.   procedure(const aArg: String)
  10.   begin
  11.     Writeln(aArg);
  12.   end('Hello World');
  13.  
  14.   p := procedure
  15.        begin
  16.              Writeln('Foobar');
  17.            end;
  18.   p();
  19.  
  20.   n := procedure(aSender: TObject);
  21.        begin
  22.              Writeln(HexStr(Pointer(aSender));
  23.            end;
  24.   n(Nil);
  25.  
  26.   f := function(MyRes : LongInt;
  27.        begin
  28.              MyRes := 42;
  29.            end;
  30.   Writeln(f());
  31. end.
« Last Edit: August 24, 2025, 11:48:34 pm by d2010 »

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: Feature announcement: Function References and Anonymous Functions
« Reply #125 on: August 25, 2025, 08:10:42 am »
The syntax is delphi compatible.
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

PascalDragon

  • Hero Member
  • *****
  • Posts: 6311
  • Compiler Developer
Re: Feature announcement: Function References and Anonymous Functions
« Reply #126 on: September 02, 2025, 09:12:13 pm »
Why You using  same syntax, perhaps overflow-up too much the tokenizer?

This feature is intended to be Delphi-compatible, thus the Delphi-compatible syntax is used. It makes no sense to invent a different syntax when we need the Delphi-compatible one anyway. Nothing more, nothing less.

cocce

  • Newbie
  • Posts: 4
Re: Feature announcement: Function References and Anonymous Functions
« Reply #127 on: January 18, 2026, 12:06:51 pm »
Do you know when it will be released?
Thanks in advance.
« Last Edit: January 18, 2026, 01:29:58 pm by cocce »

PascalDragon

  • Hero Member
  • *****
  • Posts: 6311
  • Compiler Developer
Re: Feature announcement: Function References and Anonymous Functions
« Reply #128 on: January 18, 2026, 03:59:09 pm »
Do you know when it will be released?

No.

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: Feature announcement: Function References and Anonymous Functions
« Reply #129 on: January 19, 2026, 06:55:15 am »
If you do not use FreePascal in any professional capacity, but only as a semi-professional or a hobbyist, why don't you simply use trunk?
I think that most users that call for a release that contains  certain new features really do not need a "release" for that.
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

PascalDragon

  • Hero Member
  • *****
  • Posts: 6311
  • Compiler Developer
Re: Feature announcement: Function References and Anonymous Functions
« Reply #130 on: January 20, 2026, 09:50:39 pm »
有点慢

Please only use English in the international part of the forum or provide a translation with your post.

vfclists

  • Hero Member
  • *****
  • Posts: 1157
    • HowTos Considered Harmful?
Re: Feature announcement: Function References and Anonymous Functions
« Reply #131 on: February 05, 2026, 11:10:20 am »
If you do not use FreePascal in any professional capacity, but only as a semi-professional or a hobbyist, why don't you simply use trunk?
I think that most users that call for a release that contains  certain new features really do not need a "release" for that.

Is trunk good enough and safe enough for the current Lazarus 4.xx? Are enough users happy with it?

The comments in this thread don't reassure me Status of FPC 3.4.0 or FPC 4.0.0 [major release]
Lazarus 3.0/FPC 3.2.2

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: Feature announcement: Function References and Anonymous Functions
« Reply #132 on: February 05, 2026, 11:13:00 am »
About 30-40% use trunk. For all intend and purpose it feels stable but there are many open bugs of differing urgency. Most people won't even notice any bugs.
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

440bx

  • Hero Member
  • *****
  • Posts: 6069
Re: Feature announcement: Function References and Anonymous Functions
« Reply #133 on: February 05, 2026, 11:19:45 am »
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.)

FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

ALLIGATOR

  • Sr. Member
  • ****
  • Posts: 369
  • I use FPC [main] 💪🐯💪
Re: Feature announcement: Function References and Anonymous Functions
« Reply #134 on: February 05, 2026, 11:23:40 am »
I just want to note that FPC 3.2.2 and 3.2.4 also have bugs, just like FPC[main]

And it's still a big question where there are more of them
I may seem rude - please don't take it personally

 

TinyPortal © 2005-2018