Einzelnen Beitrag anzeigen

TiGü

Registriert seit: 6. Apr 2011
Ort: Berlin
3.060 Beiträge
 
Delphi 10.4 Sydney
 
#1

E2010 - Incompatible Types-Fehler in generischer Factory

  Alt 1. Apr 2015, 11:28
Hallo zusammen,

in einen Anflug von Genie und/oder Wahnsinn wollte ich mir eine "sehr" generische Factory bauen:
Delphi-Quellcode:
unit Common.Factory;

interface

uses
  System.SysUtils,
  System.Generics.Collections;

type
  TCommonFactory<Kind, Output, TFactoryFunction, ConstructParameter> = class abstract
  public type
    TFactoryFunction = reference to function(const AConstructParameter : ConstructParameter) : Output;
  private
    class var
      FFactoryDictionary : System.Generics.Collections.TDictionary<Kind, TFactoryFunction>;
    class constructor Create;
    class destructor Destroy;
  public
    class procedure Register(const AKind : Kind; const AConstructFunction : TFactoryFunction);
    class function Get(const AKind : Kind; const AConstructParameter : ConstructParameter) : Output;
  end;

implementation

class constructor TCommonFactory<Kind, Output, TFactoryFunction, ConstructParameter>.Create;
begin
  FFactoryDictionary := System.Generics.Collections.TDictionary<Kind, TFactoryFunction>.Create;
end;

class destructor TCommonFactory<Kind, Output, TFactoryFunction, ConstructParameter>.Destroy;
begin
  FreeAndNil(FFactoryDictionary);
end;

class function TCommonFactory<Kind, Output, TFactoryFunction, ConstructParameter>.Get(const AKind : Kind; const AConstructParameter : ConstructParameter) : Output;
var
  LConstructFunction : TFactoryFunction;
begin
  Result := default (Output);
  if FFactoryDictionary.TryGetValue(AKind, LConstructFunction) then
  begin
    Result := LConstructFunction(AConstructParameter);
  end;
end;

class procedure TCommonFactory<Kind, Output, TFactoryFunction, ConstructParameter>.Register(const AKind : Kind; const AConstructFunction : TFactoryFunction);
begin
  FFactoryDictionary.Add(AKind, AConstructFunction); //<--- Fehler
end;

end.
Leider sagt mir mein XE7, dass beim Add ein E2010 vorliegt:
Code:
[dcc32 Error] Common.Factory.pas(48): E2010 Incompatible types: 'Common.Factory.TCommonFactory<Common.Factory.TCommonFactory<Kind,Output,TFactoryFunction,ConstructParameter>.Kind,
Common.Factory.TCommonFactory<Kind,Output,TFactoryFunction,ConstructParameter>.Output,
Common.Factory.TCommonFactory<Kind,Output,TFactoryFunction,ConstructParameter>.TFactoryFunction,
Common.Factory.TCommonFactory<Kind,Output,TFactoryFunction,ConstructParameter>.ConstructParameter>.TFactoryFunction' and 'TFactoryFunction'
Die Fehlermeldung ist ja schon so undurchschaubar lang.
Ich weiß, dass die Generics immer noch mit Bugs zu kämpfen haben und ich werde mein Problem wohl anders lösen müssen.

Hat jemand eine Idee, woran das liegen könnte bzw. wie ich elegant mein Problem löse?

Letztendlich geht es darum, das man ein Enum-Wert übergibt (Kind ) und man dafür ein Interface zurück bekommt (Output ).
Die Instanzen sollen über (anonyme) Factory-Funktionen (TFactoryFunction ) erstellt werden, die ein Parameter benötigen (ConstructParameter ).
  Mit Zitat antworten Zitat