[fpc-devel] Local procedures as procedural parameter
Michael Van Canneyt
michael at freepascal.org
Mon Mar 14 11:11:21 CET 2005
On Mon, 14 Mar 2005, Jonas Maebe wrote:
>
> 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.
This is clear.
>
>> 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).
No, because normally, one never mixes 'procedure of object' with 'procedural'.
You program either linear, either OOP, so you either use one or the other,
never both. The distinction is also very clear.
In the case of 'local' or 'global' this distinction is, in my opinion,
much less clear.
>
>> 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? :)
No, see above.
>> 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).
No, because in this case, you are comparing 2 basically different programming
techniques: OOP and linear; they are fundamentally different in their practical use.
I don't see much code out there which mixes the two.
(I'm talking 50-50%, not 99-1%)
Beware:
I want to make clear that I am not against the concept "an sich".
If the compiler was born 'out of the blue', and it supported 'iso'
procedures from the very start, the problem would not exist. All procedures
(local and global) could be made 'iso-aware', and you would be able to pass
both local and global functions for the same procedural parameter.
The problem is that we are in the situation where the majority of
existing code does not work like that, and that we have to introduce a
'schisma' in the compiler. I'm not particularly fond of this idea.
Michael.
More information about the fpc-devel
mailing list