Recent

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

munair

  • Hero Member
  • *****
  • Posts: 880
  • 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: 6000
  • 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: 5571
    • 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: 6000
  • 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 😅

 

TinyPortal © 2005-2018