Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2016-03

Fpc 3.0 Bug: type specialization and operator overload

3 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Mazola Winstrol

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

Sven Barth
to Mazola Winstrol
"Mazola Winstrol" wrote 6 lines
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.

[snip]

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.

It does not treat them the same, it simply picks the wrong one (as in "doesn't correctly respect the unit qualifier"). You should see a different (yet no less wrong) behavior if you turn around the units in the uses-clause.

Please report.

Regards,
Sven

Mazola Winstrol
to Sven Barth

2016-03-12 21:45 GMT-03:00 Sven Barth :

Show quoted text 11 lines
It does not treat them the same, it simply picks the wrong one (as in "doesn't correctly respect the unit qualifier"). You should see a different (yet no less wrong) behavior if you turn around the units in the uses-clause. Please report. Regards, Sven

Done. #0029859. <http://bugs.freepascal.org/view.php?id=29859>

Regards