Recent

Author Topic: How to define a type of reference to procedure  (Read 374 times)

mihaisheng

  • Newbie
  • Posts: 4
How to define a type of reference to procedure
« on: June 11, 2026, 03:11:34 am »
{$mode objfpc}{$H+}
{$modeswitch anonymousfunctions}
 
type
  TMyProc = reference to procedure;

may be lazarus4.6 cant recognize this symbol.

unittry.pas(4,2) Warning: Illegal compiler switch "ANONYMOUSFUNCTIONS+"
unittry.pas(13,12) Error: Identifier not found "reference"
unittry.pas(13,22) Error: Error in type definition
unittry.pas(13,22) Error: Syntax error, ";" expected but "TO" found

Fibonacci

  • Hero Member
  • *****
  • Posts: 1024
  • Behold, I bring salvation - FPC Unleashed
Re: How to define a type of reference to procedure
« Reply #1 on: June 11, 2026, 03:22:26 am »
Anonymous Functions and Function References were introduced around 2022, but you won't find them in any released FPC ;) The most recent FPC release is, AFAIK, 3.2.2 from 2021.

You need to build the trunk version yourself.

Also, for "reference" to work you need one more modeswitch:

Code: Pascal  [Select][+][-]
  1. {$modeswitch anonymousfunctions}
  2. {$modeswitch functionreferences}
FPC Unleashed: async/await, parallel for, match, tuples, string interpolation, inline vars, autofree, no-RTTI & tons more. Star on GitHub

mihaisheng

  • Newbie
  • Posts: 4
Re: How to define a type of reference to procedure
« Reply #2 on: June 11, 2026, 05:52:38 pm »
Lazarus:4.6,FPC:3.2.2

{$mode objfpc}{$H+}
{$modeswitch anonymousfunctions}
{$modeswitch functionreferences}

unittry.pas(4,2) Warning: Illegal compiler switch "ANONYMOUSFUNCTIONS+"
unittry.pas(5,2) Warning: Illegal compiler switch "FUNCTIONREFERENCES+"
unittry.pas(13,12) Error: Identifier not found "reference"
unittry.pas(13,22) Error: Error in type definition
unittry.pas(13,22) Error: Syntax error, ";" expected but "TO" found
Codetools,error: 1
unittry.pas(13,12) Error: not found:reference

error still alive.

such as: try.except.finally, AI tell me this way was always supported in FPC3.2.2,but im still use the way of  try.try.except.end.finally.end.

welcome.


Fibonacci

  • Hero Member
  • *****
  • Posts: 1024
  • Behold, I bring salvation - FPC Unleashed
Re: How to define a type of reference to procedure
« Reply #3 on: June 11, 2026, 05:57:12 pm »
3.2.2 doesn't have it - no official release has these features yet. You have to build FPC yourself, from source.

Easiest way is fpcupdeluxe: https://github.com/LongDirtyAnimAlf/fpcupdeluxe/releases
FPC Unleashed: async/await, parallel for, match, tuples, string interpolation, inline vars, autofree, no-RTTI & tons more. Star on GitHub

mas steindorff

  • Hero Member
  • *****
  • Posts: 601
Re: How to define a type of reference to procedure
« Reply #4 on: June 11, 2026, 09:21:58 pm »
Code: Pascal  [Select][+][-]
  1. type
  2.   TMyProc = procedure of object;
  3.  
is what I've used for years but I do not know if that is what you are looking for.
 
Mas
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

PascalDragon

  • Hero Member
  • *****
  • Posts: 6403
  • Compiler Developer
Re: How to define a type of reference to procedure
« Reply #5 on: June 11, 2026, 09:24:45 pm »
such as: try.except.finally, AI tell me this way was always supported in FPC3.2.2,but im still use the way of  try.try.except.end.finally.end.

Believe us, not some AI. Anonymous functions and function references are only available in 3.3.1 and a shorthand combination for tryfinally and tryexcept does not exist at all.

Also please use [code][/code]-tags for better readability.

 

TinyPortal © 2005-2018