-4

Part of the code in which the error:

 function ReadBytes(maxLen: integer): TBytes; virtual;
...
    uses sysutils, windows, SysConst, commutil;
...
    function TBasicBufferedStream.ReadBytes(maxLen: Integer): TBytes;
    begin
      if maxLen > 0 then
      begin
        if maxLen <= (fBufFill - fInBufPos) then
        begin
          SetLength(result, maxLen);
          Move(fBuf[fInBufPos], result[0], maxLen);
          inc(fInBufPos, maxLen);
        end
        else
        begin
          result := Peek(maxLen);
          Skip(maxLen);
        end;
      end
      else
        result := nil;
    end;

In Delphi 10.2 it doesn't compile, in Delphi 7 it compiled!

What is my mistake that I do not understand?

Nasreddine Galfout
  • 2,550
  • 2
  • 18
  • 36
Error404
  • 9
  • 8

1 Answers1

2

This is just an educated guess as the provided code is sparse. The small hints the provided code gave:

  • uses provided in the code sample after the declaration contain System.SysUtils, the unit where TBytes is defined
  • The provided uses are after the declaration, so TBytes there must have a different source than System.SysUtils

Therefore it is likely that TBytes is not actually referring to the same type in declaration and implementation part. This can be visualized for example by hovering the mouse over a type. The tool tip will tell you what the exact type is the compiler is referring to.

I can for example reproduce your problem with two small units. TBytes is declared in System.SysUtils, but I declare another one - like it is defined in Delphi 2009 (see below) in Unit3:

unit Unit3;

interface

type
  TBytes = array of Byte;

implementation

end.

When I now create a unit like the following, I'm mixing up the usage of TBytes from two different Units, that aren't compatible:

unit Unit2;

interface

uses
  Unit3;

  function ReadBytes(maxLen: integer): TBytes;

implementation

uses
  System.SysUtils;

function ReadBytes(maxLen: integer): TBytes;
begin
  //
end;

end.

The types that the tool tip will show are type Unit3.TBytes: array of Byte and type System.SysUtils.TBytes: System.Array<System.Byte>.

So in fact the signature of my function in the declaration differs from the signature in the implementation.

This can be resolved by

  • Inspecting if the used units are actually correct and needed, can you get rid of the one causing ambiguity?
  • If not possible to solve it with the first point, it is possible to refer explicitly to which type is meant by prefixing with the containing unit:

      function ReadBytes(maxLen: integer): Unit3.TBytes;
    

I looked at the history of System.SysUtils.TBytes retroactively, couldn't find it for Delphi 7, but in Delphi 2009 the definition of TBytes was following: TBytes = array of Byte; I have changed my code example with that in mind and rephrased part of the answer.

nil
  • 1,320
  • 1
  • 10
  • 21
  • Or just remove `System.SysUtils;` from the implementation section to use your defined type – Nasreddine Galfout Oct 02 '17 at 12:21
  • @NasreddineAbdelillahGalfout This is basically _Inspecting if the used units are actually correct and needed, can you get rid of the one causing ambiguity?_, isn't it? – nil Oct 02 '17 at 12:23
  • it worked on D7 so this case is not possible I guess( I'm not sure if TBytes was part of System.SysUtils in D7 though) – Nasreddine Galfout Oct 02 '17 at 12:51
  • @NasreddineAbdelillahGalfout Generics were introduced with 2009, the current definition (and at least back to XE2) of TBytes is using generics. Delphi 7 was at least not using the same TBytes. – nil Oct 02 '17 at 13:02
  • Here is the solution - dyb.TBytes – Error404 Oct 02 '17 at 13:14