[fpc-devel] Local procedures as procedural parameter

Jonas Maebe jonas at zeus.ugent.be
Mon Mar 14 10:59:09 CET 2005


On 14 mrt 2005, at 10:48, Michael Van Canneyt wrote:

>>> Procedure iso_fun( function f( x: real): real);
>>> begin
>>>   StoredF:=F;
>>> end;
>>
>> This assignment is invalid, it's the same as trying to assign a 
>> method to a regular procedural variable.
>
> This is exactly my point: how does the compiler distinguish the two in 
> this case ?

The same way it distinguishes between a "procedure x(...) of object" 
and a "procedure x(...)". When parsing the function header and 
encountering such a declaration, it sets some sort of flag inside the 
procdef that this is an ISO-style procvar. Later, when trying to assign 
this to a regular procvar, you'll get a type clash.

> I don't see how, unless you add an extra parameter for the 'iso' way of
> doing things.
>
> So, basically, to be able to use a local procedure as a callback,
> - You must declare a function with the ISO way to specify a callback.
> - This function is not usable with a global callback function.
> -> So if you would want to use such a function for both normal and 
> local
>   procedures, you would have to implement it twice !

Just like you have to implement functions which you want to be able to 
use as both "procedure of object" and regular procedural variables 
twice. We have similar problems in the compiler, with e.g. foreach and 
foreach_static (and foreachnodestatic).

> If this is indeed the case, then it is totally ridiculous in my opinion
> to implement such a "feature", because if you must make special amends
> _anyway_, then you can just as well do it with global functions, 
> without
> the local variants.

So we do away with procedure of object also? :)

> I question the usefullness of this whole "feature".

The usefulness is mainly existing code which depends on it. There was 
no procedure of object in those days. This is like saying that nested 
procedures are useless because you can make a class with methods to 
achieve the same result (having multiple functions access the same 
state, while protecting it from outside prying eyes).


Jonas





More information about the fpc-devel mailing list