[fpc-pascal] Local procedures as actual procedure parameter in macpas mode
Jonas Maebe
jonas.maebe at elis.ugent.be
Tue Aug 24 22:44:38 CEST 2010
On 24 Aug 2010, at 22:38, Alberto Narduzzi wrote:
>> Trying to compile the following code (porting from MW Pascal)
>> procC (procedure procD (var y: myObject));
>
> the compiler is complaining because the ProcB is a nested procedure of ProcA.
No. He's using MacPas mode in FPC 2.5.1. In that mode, nested procedural variables are supported (and also in ISO mode, and also in all other syntax modes if you add {$modeswitch nestedprocvars}). The error message was caused by a compiler bug.
> I also suggest you do
>
> Type TMyProc Procedure(var y:myObject);
> (*) in case of an object method, I think you must add "Of Object" at the end of its declaration.
>
> it makes all more readable on first instance.
This declares a regular procvar, while the original declaration declares a nested procvar. Declaring it as a separate type would have to be done like this:
Type TMyProc Procedure(var y:myObject) is nested;
Note that you can assign both global and nested/local procedures to a nested procedural variable (and also call both using such a procvar). And be careful when using a separate "is nested" type, because then you can construct code that calls a nested procedural variable outside the scope where it is valid (which will result in undefined behaviour).
Joans
More information about the fpc-pascal
mailing list