[fpc-devel] Local procedures as procedural parameter

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


On 14 mrt 2005, at 09:45, Michael Van Canneyt wrote:

>> 7. Consider the following program:
>>
>> 	program func;
>>
>> 	type tfun = function( x: real): real;
>>
>> 	procedure iso_fun( function f( x: real): real);
>> 	begin
>> 	end;
>>
>> 	procedure typ_fun( pf: tfun);
>> 	begin
>> 	end;
>>
>> 	procedure somefun;
>>
>> 		function f( x: real): real;
>> 		begin
>> 		 	f:= x
>> 		end;
>>
>> 	begin
>> 		iso_fun( f);
>> 		typ_fun( f); {procedural variable can't get nested routiine}
>> 	end;
>>
>> 	begin
>> 	end.
>
> 1. What happens if f would use a variable from somefun, and f is called
>    when somefun is no longer executed ?

That is not possible. If you take the address of f and put it in a 
procedural variable, you'll get an error (just like when you attempt to 
assign a method to a regular procedural variable). You can only pass it 
as parameter to procedures declared with an ISO-style 
procedure/function parameter, and since its scope is limited to 
somefun, somefun will always be active when you can do so.

> 2. I see no difference whatsoever between typ_fun and iso_fun, except
>    the use of an extra type, which, in my opinion, does not change
>    anything to the usage or code of these functions. If one is allowed,
>    the other should be allowed as well.

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.


Jonas





More information about the fpc-devel mailing list