[fpc-pascal] Callbacks as nested functions

Michael Van Canneyt michael at freepascal.org
Mon Oct 23 16:05:52 CEST 2017



On Mon, 23 Oct 2017, Ryan Joseph wrote:

>
>
>> On Oct 23, 2017, at 8:55 PM, Mattias Gaertner <nc-gaertnma at netcologne.de> wrote:
>> 
>> program test1;
>> 
>> {$mode objfpc}{$H+}
>> {$ModeSwitch nestedprocvars}
>> 
>> type
>>  TCallback = procedure (a: integer) is nested;
>> 
>> procedure DoCallback;
>>  procedure DoThis(a: integer);
>>  begin
>>    writeln(a);
>>  end;
>> var
>>  callback: TCallback;
>> begin
>>  callback := @DoThis;
>>  callback(100);
>> end;
>> 
>> begin
>>  DoCallback;
>> end.
>> 
>> Keep in mind what Sven wrote. You cannot use "callback" outside of
>> DoCallBack, because FPC does not yet support Delphi's "reference to".
>
> Thanks, I never heard of nestedprocvars. Why isn’t that the default? If I have a callback I may have it be nested or not so I always want “is nested” on I guess.

It is not the default because neither Delphi nor TP allow this.

Macpas mode enables it, since it was allowed in MWP.

Michael.


More information about the fpc-pascal mailing list