Search code examples
delphispring4d

Spring4D TMultiMap overloaded constructor not seen by compiler (Error E2250)


I'm struggling with Spring4D (1.2.2) TMultiMap generic class. I want to call an overloaded constructor and the compiler complain:

"E2250 There is no overloaded version of 'Create' that can be called with these arguments"

The argument is the correct type according to the Spring4D source code.

I devised a small do-nothing program reproducing the error:

program Spring4DMultiMapTest;

{$APPTYPE CONSOLE}

{$R *.res}

uses
    System.SysUtils,
    Generics.Collections,
    Spring.Tests.Interception.Types,
    Spring.Collections.MultiMaps,
    Spring.Collections;

type
    TMyKey = TPair<Double, Integer>;

    TMyKeyEqualityComparer = class(TInterfacedObject, IEqualityComparer<TMyKey>)
        function Equals(const Left, Right: TMyKey): Boolean; reintroduce;
        function GetHashCode(const Value: TMyKey): Integer; reintroduce;
    end;

function TMyKeyEqualityComparer.Equals(const Left, Right: TMyKey): Boolean;
begin
    if Left.Key < (Right.Key - 1E-9) then
        Result := TRUE
    else if Left.Key > (Right.Key + 1E-9) then
        Result := FALSE
    else if Left.Value < Right.Value then
        Result := TRUE
    else
        Result := FALSE;
end;

function TMyKeyEqualityComparer.GetHashCode(const Value: TMyKey): Integer;
begin
    Result := Value.Value + Round(Value.Key * 1E6);
end;

var
    Events      : IMultiMap<TMyKey, Integer>;
    KeyComparer : IEqualityComparer<TMyKey>;
begin
    KeyComparer := TMyKeyEqualityComparer.Create;
    // Next line triggers error: "E2250 There is no overloaded version of 'Create' that can be called with these arguments"
    Events := TMultiMap<TMyKey, Integer>.Create(KeyComparer);
end.

In Spring4D source code, I find the following declaration:

TMultiMap<TKey, TValue> = class(TMultiMapBase<TKey, TValue>)

and also the TMultiMap acestor is declared:

  TMultiMapBase<TKey, TValue> = class abstract(TMapBase<TKey, TValue>,
    IMultiMap<TKey, TValue>, IReadOnlyMultiMap<TKey, TValue>)

and has this constructor:

constructor Create(const keyComparer: IEqualityComparer<TKey>); overload;

This is the constructor I want to call. As far as I understand, my argument KeyComparer has the correct type. But obviously the compiler doesn't agree :-(

How to fix this code?


Solution

  • You use different IEqualityComparer<T> types.

    The IEqualityComparer<T> in your example comes from Spring.Tests.Interception.Types.pas. The one used in TMultiMap<TKey, TValue>.Create constructor is defined in System.Generics.Defaults.pas.

    If you change your uses part to

    uses
        System.SysUtils,
        Generics.Collections,
        // Spring.Tests.Interception.Types,
        Generics.Defaults,
        Spring.Collections.MultiMaps,
        Spring.Collections;
    

    your example will compile.

    Tested with Delphi 10.3 U3 and Spring4D 1.2.2