[fpc-pascal] why can't we define class operator for old fashion object type, but ok for 'advanced record' type?

Sven Barth pascaldragon at googlemail.com
Thu Aug 16 16:50:45 CEST 2018


Am 16.08.2018 um 12:47 schrieb Michael Van Canneyt:
>> Am 15.08.2018 um 17:29 schrieb Michael Van Canneyt:
>>>
>>> On Wed, 15 Aug 2018, Sven Barth via fpc-pascal wrote:
>>>
>>>>> But maybe you are using mode delphi ? If so, try separating out the
>>>>> object
>>>>> definition in a separate unit which is not compiled in delphi mode.
>>>> A global operator won't help at all as (currently) the operator won't
>>>> be visible during the specialization. Only if the operator is visible
>>>> during the *generic's* declaration it would be picked up.
>>> That is contrary to the class operator, which is also only visible
>>> during
>>> specialization, after all.
>>>
>>> Is there a reason for this behaviour ?
>> The operator of a record is visible together with the record, however a
>> global operator is not. When specializing the unit scope of the
>> declaration of the *generic* is restored thus no helpers or global
>> operators of the current scope are visible except they are part of the
>> type parameter.
>> I do want to try to change this in the future, but I need to be careful
>> to not open a different can of worms (as Delphi doesn't do it this way).
> Purely reasoning in terms of data structures:
>
> I would think you 'attach' operators to the type for which they are defined ?
> If the 'attach' link structure contains the unit in which the operator
> definition resides, you can check whether this unit is visible, and if
> yes, the operator is 'usable'.
No, they are not attached. Remember that global operators don't need to 
reside in the same unit as the type they're operating on. And it would 
be rather useless time to search for existing operator overloads for any 
type when the program uses none of them in the end. Thus the compiler 
looks for operators on the fly once it encounters the use of one, it's 
just like looking for method calls with a slightly different syntax. The 
scope of a structured type is always looked in first and then all units 
in the scope are searched for global operators.

Regards,
Sven



More information about the fpc-pascal mailing list