[fpc-devel] Local procedures as procedural parameter

Michael Van Canneyt michael at freepascal.org
Mon Mar 14 10:48:09 CET 2005



On Mon, 14 Mar 2005, Jonas Maebe wrote:

>
> On 14 mrt 2005, at 10:15, Michael Van Canneyt wrote:
>
>> It seems to me that the following is perfectly valid code :
>> 
>> Var
>>   StoredF : Function (x : real) : real
>
> This is a regular procedural variable, not an ISO-style procedural variable. 
> Just like "var a: array of byte;" is a dynamic array and not an open array.

I know.

>
>> 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 ?

>
>> StoredF can then be called at will, even outside somefun.
>> 
>> When compiling this, there is no way the compiler can determine whether
>> f (or storedf) will be a local procedure or a global one. To solve this,
>> it would mean that each procedure variable would have to consist of 2
>> things; A pointer and a type indicator.
>> 
>> This would break _a lot_ of existing code. If that is the consequence, I
>> am heavily against introducing this possibility.
>
> That is not necessary.

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 !

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.

I would only see the use of being able to pass a local function as a callback
if the called function can be used for both local and global callback procedures.

>
>>> Well, it currently isn't in either our compiler or in Delphi. You cannot 
>>> declare a function type inside a parameter list for now. Maybe this was 
>>> even done on purpose to avoid clashes with ISO-style procedure parameters.
>> 
>> Yes, but why would one be allowed and the other not ?
>
> In general, you cannot declare complex types inside a parameter list (you 
> also can't declare new record, array or set types inside a parameter list). 
> That's why you can (ab)use those things to declare special things, like open 
> arrays (and ISO-style procvars).

I understand that.

I question the usefullness of this whole "feature".

Michael.




More information about the fpc-devel mailing list