[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
Wed Aug 15 13:46:00 CEST 2018


Am 15.08.2018 um 11:17 schrieb Michael Van Canneyt:
>
>
> On Wed, 15 Aug 2018, Dennis wrote:
>
>>
>>
>> Michael Van Canneyt wrote:
>>>
>>>
>>> On Wed, 15 Aug 2018, Dennis wrote:
>>>
>>>> I was trying to use a generic class TDictionary<T>  with type T. 
>>>> This class has a method that compares a variable of T with another 
>>>> one.
>>>>
>>>> When I specialize a class using this TDictionary with a type:
>>>>
>>>> TSecurity = object
>>>>   ....
>>>> end;
>>>>
>>>>
>>>> e.g. TNewDict = class(TDictionary<String, TSecurity>);
>>>>
>>>> it raise a compiler complaining that my TSecurity type has no 
>>>> operator = defined.
>>>>
>>>> But when I try to add like this:
>>>>
>>>> TSecurity = object
>>>>   ....
>>>> class operator =  (constref aLeft, aRight: TSecurity ): Boolean; 
>>>> //<---compiler Error: Procedure or Function expected
>>>>
>>>> end;
>>>
>>> Did you try creating an "old-fashioned" = operator ?
>>>
>>> Something like
>>>
>>> Operator = (l,r : TSecurity) z : boolean;
>>>
>>> begin
>>>   // compare here
>>> end;
>>>
>>
>> Just tried.
>> It complained:   Fatal: Syntax error, "IMPLEMENTATION" expected but 
>> "identifier OPERATOR" found
>
> Following works here:
>
> Prograp testo;
>
> Type
>   TSecurity = Object
>     a,b : Integer;
>   end;
>
> Operator = (l,r : TSecurity) z : boolean;
>
> begin
>   Z:=(l.a=r.a) and (L.b=r.b);
> end;
>
> begin
> end.
>
> 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.

Regards,
Sven



More information about the fpc-pascal mailing list