Lazarus

Free Pascal => FPC development => Topic started by: cai on April 01, 2023, 05:23:25 am

Title: [Function References ] not work on MACOS FPC 3.3.1
Post by: cai on April 01, 2023, 05:23:25 am
{$modeswitch functionreferences}

=============================
type
  TProc =reference to procedure;
========================
Compile & print out
Error: Identifier not found "reference "
Title: Re: [Function References ] not work on MACOS FPC 3.3.1
Post by: cai on April 01, 2023, 06:37:46 am
OK,I was wrong!
it is cause by {$mode objfpc}


===================
failed code
---------
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$modeswitch functionreferences}
  4. {$mode objfpc}{$H+}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     procedure FormCreate(Sender: TObject);
  17.   private
  18.  
  19.   public
  20.  
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.  
  26. implementation
  27.  
  28. {$R *.frm}
  29.  
  30. type
  31.   TProc =reference to procedure;
  32.  
  33. { TForm1 }
  34.  
  35. procedure TForm1.FormCreate(Sender: TObject);
  36. var
  37.   P: TProc;
  38. begin
  39.  
  40. end;
  41.  
  42. end.
  43.  



==============
succeed code
---------------
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$modeswitch functionreferences}
  4. //{$mode objfpc}
  5. {$H+}
  6.  
  7. interface
  8.  
  9. uses
  10.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs;
  11.  
  12. type
  13.  
  14.   { TForm1 }
  15.  
  16.   TForm1 = class(TForm)
  17.     procedure FormCreate(Sender: TObject);
  18.   private
  19.  
  20.   public
  21.  
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.  
  27. implementation
  28.  
  29. {$R *.frm}
  30.  
  31. type
  32.   TProc =reference to procedure;
  33.  
  34. { TForm1 }
  35.  
  36. procedure TForm1.FormCreate(Sender: TObject);
  37. var
  38.   P: TProc;
  39. begin
  40.  
  41. end;
  42.  
  43. end.
  44.  
  45.  

why?
Title: Re: [Function References ] not work on MACOS FPC 3.3.1
Post by: cai on April 01, 2023, 06:44:18 am
I was wrong! again! :-[
{$mode objfpc} should be put in front of {$modeswitch}

Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2. {$modeswitch functionreferences}
  3.  
Title: Re: [Function References ] not work on MACOS FPC 3.3.1
Post by: Thaddy on April 01, 2023, 09:43:45 am
The reason being that {$mode} is actually a set of {$modeswitches}.
It is documented that for the time being {$modeswitch functionreferences} does not belong to any mode yet.
This may change in the future, though.

https://wiki.freepascal.org/FPC_New_Features_Trunk#Support_for_Function_References_and_Anonymous_Functions
https://forum.lazarus.freepascal.org/index.php/topic,59468.0.html

It is likely the modeswitch  will part of {$mode delphi} and {$mode objfpc} in the future.
https://forum.lazarus.freepascal.org/index.php/topic,59468.msg443373.html#msg443373
( but that' s why it is in trunk only: it needs testing and it needs to mature )
Title: Re: [Function References ] not work on MACOS FPC 3.3.1
Post by: PascalDragon on April 01, 2023, 04:23:12 pm
I was wrong! again! :-[
{$mode objfpc} should be put in front of {$modeswitch}

Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2. {$modeswitch functionreferences}
  3.  

A {$mode ...} directive resets any {$modeswitch ...} directive.
TinyPortal © 2005-2018