Delphi Error – E2008 Incompatible types

Delphi Compiler Error

E2008 Incompatible types

Reason for the Error & Solution

This error message occurs when the compiler expected two types to be compatible (meaning very similar), but in fact, they turned out to be different. This error occurs in many different situations – for example when a read or write clause in a property mentions a method whose parameter list does not match the property, or when a parameter to a standard procedure or function is of the wrong type.

This error can also occur when two units both declare a type of the same name. When a procedure from an imported unit has a parameter of the same-named type, and a variable of the same-named type is passed to that procedure, the error could occur.

unit unit1;
interface
  type
    ExportedType = (alpha, beta, gamma);

implementation
begin
end.

unit unit2;
interface
  type
    ExportedType = (alpha, beta, gamma);

  procedure ExportedProcedure(v : ExportedType);

implementation
  procedure ExportedProcedure(v : ExportedType);
  begin
  end;

begin
end.

program Produce;
uses unit1, unit2;

var
  A: array [0..9] of char;
  I: Integer;
  V : ExportedType;
begin
  ExportedProcedure(v);
  I:= Hi(A);
end.

The standard function Hi expects an argument of type Integer or Word, but we supplied an array instead. In the call to ExportedProcedure, V actually is of type unit1.ExportedType since unit1 is imported prior to unit2, so an error will occur.

unit unit1;
interface
  type
    ExportedType = (alpha, beta, gamma);

implementation
begin
end.

unit unit2;
interface
  type
    ExportedType = (alpha, beta, gamma);

  procedure ExportedProcedure(v : ExportedType);

implementation
  procedure ExportedProcedure(v : ExportedType);
  begin
  end;

begin
end.

program Solve;
uses unit1, unit2;
var
  A: array [0..9] of char;
  I: Integer;
  V : unit2.ExportedType;
begin
  ExportedProcedure(v);
  I:= High(A);
end.

We really meant to use the standard function High, not Hi. For the ExportedProcedure call, there are two alternative solutions. First, you could alter the order of the uses clause, but it could also cause similar errors to occur. A more robust solution is to fully qualify the type name with the unit which declared the desired type, as has been done with the declaration for V above.

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

Delphi Compiler Error E2313 Attribute – Known attribute cannot specify properties Reason for the Error & Solution No further information...
Delphi Compiler Error E2379 Virtual methods not allowed in record types Reason for the Error & Solution No further information...
Rodrigo , one of the long time Delphi Developer has been working on one of his personal project “Delphi IDE...