Why can't the compiler find an overloaded version of my function?

Without being Getoverloaded (using only the first definition Get), otherwise ok is compiled:

program Project1;
{$APPTYPE CONSOLE}

uses SysUtils, Classes;

type
  TxALNameValuePair = record
    Name: ansistring;
    Value: ansistring;
    constructor Create(const AName, AValue: ansistring);
  end;
  TxALNameValueArray = TArray<TxALNameValuePair>;

  TxALHTTPClient = class(TObject)
    private
    protected
    public
      Function  Get(const aUrl:AnsiString;
                    const ARequestHeaderValues: TxALNameValueArray = nil): AnsiString; overload;  
      Function  Get(const aUrl:AnsiString;
                    const aRequestFields: TStrings;
                    const aEncodeRequestFields: Boolean=True;
                    const ARequestHeaderValues: TArray<TxALNameValuePair> = nil): AnsiString; overload;      
  end;

constructor TxALNameValuePair.Create(const AName, AValue: ansiString);
begin
  Name := AName;
  Value := AValue;
end;       

function TxALHTTPClient.Get(const aUrl: AnsiString;
  const ARequestHeaderValues: TxALNameValueArray): AnsiString;
begin    
end;

Function  TxALHTTPClient.Get(const aUrl:AnsiString;
              const aRequestFields: TStrings;
              Const aEncodeRequestFields: Boolean=True;
              const ARequestHeaderValues: TArray<TxALNameValuePair> = nil): AnsiString;
begin
end;

var
  aHttpCLient: TxALHTTPClient;
begin
  aHttpClient := TxALHTTPClient.Create;
  aHttpCLient.get('http://www.toto.com', [TxALNameValuePair.Create('Accept-Encoding', 'gzip')]);
  ReadLn;
end.

But when it is Getoverloaded, it produces

[dcc64 error] E2250 Overload missing version of "Get" that can be called using these arguments

Why can't the compiler solve this problem?

+4
source share
1 answer

The problem is that the dynamic array constructor you are using creates an object of type array of TxALNameValuePair, but all of your overloads require the type to be TxALNameValueArray, and it seems that the compiler is not making a connection between array of TxALNameValuePair==> TArray<T>for T => TAxTxALNameValuePair.

, , , , - . (, , ) - .

var
  aHttpCLient: TxALHTTPClient;
  nvpArray : TxALNameValueArray;
begin
  aHttpClient := TxALHTTPClient.Create;
  nvpArray := [TxALNameValuePair.Create('Accept-Encoding', 'gzip')];
  aHttpCLient.get('http://www.toto.com', nvpArray);
  ReadLn;
end.

:

var
  aHttpCLient: TxALHTTPClient;
begin
  aHttpClient := TxALHTTPClient.Create;
  aHttpCLient.get('http://www.toto.com', 
                  TxALNameValueArray.Create(
                    TxALNameValuePair.Create('Accept-Encoding', 'gzip')
                  ));
  ReadLn;
end.

, , , System.Net.URLClient:

program Project1;
{$APPTYPE CONSOLE}

uses SysUtils, Classes, System.Net.URLClient;

type
  TxALHTTPClient = class(TObject)
    private
    protected
    public
      Function  Get(const aUrl:AnsiString;
                    const ARequestHeaderValues: TNameValueArray = nil): AnsiString; overload;
      Function  Get(const aUrl:AnsiString;
                    const aRequestFields: TStrings;
                    const aEncodeRequestFields: Boolean=True;
                    const ARequestHeaderValues: TNameValueArray = nil): AnsiString; overload;
  end;

function TxALHTTPClient.Get(const aUrl: AnsiString;
  const ARequestHeaderValues: TNameValueArray): AnsiString;
begin
end;

Function  TxALHTTPClient.Get(const aUrl:AnsiString;
              const aRequestFields: TStrings;
              Const aEncodeRequestFields: Boolean=True;
              const ARequestHeaderValues: TNameValueArray = nil): AnsiString;
begin
end;

var
  aHttpCLient: TxALHTTPClient;
begin
  aHttpClient := TxALHTTPClient.Create;
  aHttpCLient.get('http://www.toto.com', [TNameValuePair.Create('Accept-Encoding', 'gzip')]);
  ReadLn;
end.

, - , .

TNetHeaders TNameValueArray, . ,

TxALNameValueArray = TNetHeaders;

.


, , :

program Project1;
{$APPTYPE CONSOLE}

type
  TDblArray = TArray<double>;

procedure A(i : integer; da : TDblArray); overload;
begin
end;

procedure A(s : string; da : TDblArray); overload;
begin
end;

begin
  A(1, [1.0]);
end.

.

, :

program Project1;
{$APPTYPE CONSOLE}

type
  TDblArray = array of double;

procedure A(i : integer; da : TDblArray); overload;
begin
end;

procedure A(s : string; da : TDblArray); overload;
begin
end;

begin
  A(1, [1.0]);
end.

:

program Project1;
{$APPTYPE CONSOLE}

uses Types;

procedure A(i : integer; da : TDoubleDynArray); overload;
begin
end;

procedure A(s : string; da : TDoubleDynArray); overload;
begin
end;

begin
  A(1, [1.0]);
end.

, ? . , ... ​​ . QP, .

+7

Source: https://habr.com/ru/post/1694307/


All Articles