[fpc-devel] nestedprocvar questions

Sven Barth pascaldragon at googlemail.com
Tue Jul 31 22:11:17 CEST 2012


On 31.07.2012 19:04, David Jenkins wrote:
> Some questions about 'nestedprocvars' mode switch
>
> We have a simplified version of the nested procedure calling
> demonstrated in tests/test/tmaclocalprocparam3d.pp.  It looks more or
> less like:
>
> {$mode DELPHI}
> {$modeswitch nestedprocvars}
>
> type
>    tnestedprocvar = function(AArg: string): boolean;
>
>
>    procedure p1(AArg: string; pp: tnestedprocvar);
>    begin
>      if pp(AArg) then
>        writeln('true');
>    end;
>
>    procedure q;
>
>      function r(AArg: string): boolean;
>      begin
>        writeln(AArg)
>        Result := true;
>      end;
>
>    begin
>       p1('test string', @r);
>    end;
>
> Notice that it is $MODE DELPHI, that the proc var is a function, and
> that we do not access any external variables in the nested function.
>
> Question #1:
> There seems to be an interaction between $MODE DELPHI and the 'is
> nested' construct.  I.e. I cannot put the 'is nested' after the
> tnestedprocvar type and do the function passing with '@r'.  If I do the
> compiler complains about getting a pointer (@r) when it was expecting a
> function (..): boolean is nested.  So either I remove the'@' and just
> pass 'r' or I do not include the 'is nested'.

Delphi does not use the "@" to designate the function address, so this 
isn't to take the address of a function/method. In Delphi you just use 
"r" (in your example).

> Is this correct?
>
> Question #2:
> This code is being compiled both with Delphi compiler and with FPC.
> Delphi handles the nesting with no problem and with no 'is nested'
> directive.
>
> Should nestedprocvar be included automatically with $MODE DELPHI for
> compatibility?
>

Adding "nestedprocvar" automatically wouldn't change anything as you'd 
still need "is nested" to have a procvar to which nested functions can 
be passed to.

> Question #3:
> If we set up our code as above can we expect the compiler to be
> consistent in parameter set up for the next while?

How exactly do you mean this?

> Question #4:
> If we set up our code as above (with no 'is nested') would it be safe to
> include access of an external variable (internal to procedure q) to the
> nested function?

The whole purpose of the "is nested" is to allow access to such 
variables of the parent frame, so without it accessing the nested 
function will result in problems.

Regards,
Sven




More information about the fpc-devel mailing list