Recent

Author Topic: Feature announcement: Function References and Anonymous Functions  (Read 78974 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: 6195
  • 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: 5615
    • 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: 6195
  • 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

  • Full Member
  • ***
  • Posts: 230
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: 18356
  • Here stood a man who saw the Elbe and jumped it.
Re: Feature announcement: Function References and Anonymous Functions
« Reply #125 on: August 25, 2025, 08:10:42 am »
The syntax is delphi compatible.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

PascalDragon

  • Hero Member
  • *****
  • Posts: 6195
  • 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.

 

TinyPortal © 2005-2018