[fpc-pascal] Fpc 3.0 Bug: type specialization and operator overload

Mazola Winstrol mazofeifer at gmail.com
Sat Mar 12 22:45:13 CET 2016


Hello,

Fpc version: 3.0 (the one provided by Lazarus).

Please confirm if this is a bug regarding operator overloading and type
specialization. It is a quite difficult to explain, but i will try.


Consider this unit

=== CODE ===

unit MyRecordDefinitionA;

{$mode delphi}

interface

type
  TMyRecord<T> = record
  public
    FValue: T;
    class operator Add(A,B: TMyRecord<T>): TMyRecord<T>;
  end;

implementation

class operator TMyRecord<T>.Add(A,B: TMyRecord<T>): TMyRecord<T>;
begin
  Result.FValue := A.FValue + B.FValue;
end;

end.

=== END ===

The unit above defines a generic type named TMyRecord with the arithmetic
operator overloaded Add. This type is supposed to be specialized with a
type parameter that supports the add operation (e.g Integer types).


=== CODE ===

unit MyRecordDefinitionB;

{$mode delphi}

interface

type
  TMyRecord<T> = record
  public
    FValue: T;
    class operator LogicalAnd(A: TMyRecord<T>; B: Boolean): TMyRecord<T>;
  end;

implementation

class operator TMyRecord<T>.LogicalAnd(A: TMyRecord<T>; B: Boolean):
TMyRecord<T>;
begin
  Result.FValue := A.FValue and B;
end;

end.

=== END ===


The unit above defines a generic type named TMyRecord too, but with the
logical operator overloading And. This type is supposed to be specialized
with a type parameter that supports the logical and operation (e.g Boolean
types).


=== CODE ===

unit MyRecordSpecialization;

{$mode delphi}

interface

uses
  MyRecordDefinitionA, MyRecordDefinitionB;

type
  TMyIntegerRecord = MyRecordDefinitionA.TMyRecord<Integer>;
  TMyBooleanRecord = MyRecordDefinitionB.TMyRecord<Boolean>;

implementation

end.


=== END ===


The unit above defines two specialized types based on the generic type
TMyRecord defined in the units MyRecordDefinitionA and MyRecordDefinitionB.

When i try to compile MyRecordSpecialization, i get this error: "Operator
is not overloaded: LongInt and Boolean".

It seems the compiler treats the type MyRecordDefinitionA.TMyRecord and
MyRecordDefinitionB.TMyRecord as being the same type. So, the compiler try
to apply the logical operator and of MyRecordDefinitionB.TMyRecord in the
integer specialization of the type MyRecordDefinitionA.TMyRecord.

Regards
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20160312/d20a89eb/attachment.html>


More information about the fpc-pascal mailing list