Autor Téma: Generika a hláška "incompatible types"  (Přečteno 254 krát)

Offline Morrison

  • Hrdina
  • ****
  • Příspěvků: 414
  • Karma: 12
    • Verze Delphi: D5, XE2, 10.4.2, D11
Generika a hláška "incompatible types"
« kdy: 12-10-2022, 09:56:56 »
Zdravím,
můžete mi někdo vysvětlit, proč mi kompilátor řve na označené řádce? Asi mi to takhle po ránu ještě nemyslí, nebo nevím...

Kód: Delphi [Vybrat]
  1. unit CommonDataStore;
  2.  
  3. interface
  4.  
  5. uses
  6.   System.Generics.Collections;
  7.  
  8. type
  9.   TDataStoreEntry = class;
  10.  
  11.   TDataStore<T: TDataStoreEntry> = class(TObjectDictionary<Integer, T>)
  12.   private
  13.   protected
  14.     function GetEntry(ID: Integer): T; virtual;
  15.   public
  16.     constructor Create; reintroduce;
  17.     destructor Destroy; override;
  18.     function AddEntry(NewEntry: T): Boolean; virtual;
  19.     procedure DeleteEntry(const ID: Integer); overload; virtual;
  20.     procedure DeleteEntry(Entry: TDataStoreEntry); overload; virtual;
  21.     property Entry[ID: Integer]: T read GetEntry; default;
  22.   end;
  23.  
  24.   TDataStoreEntry = class
  25.   private
  26.     FID: Integer;
  27.     FOwnerDataStore: TDataStore<TDataStoreEntry>;
  28.   protected
  29.     function GetDataStore: TDataStore<TDataStoreEntry>; virtual;
  30.     function GetIsNull: Boolean; virtual;
  31.     function Delete: Boolean; virtual;
  32.   public
  33.     property ID: Integer read FID write FID;
  34.     property IsNull: Boolean read GetIsNull;
  35.   end;
  36.  
  37.   var NULL_DATASTORE_ENTRY: TDataStoreEntry;
  38.  
  39. implementation
  40.  
  41. uses
  42.   System.Generics.Defaults;
  43.  
  44. { TDataStore<T> }
  45.  
  46. function TDataStore<T>.AddEntry(NewEntry: T): Boolean;
  47. begin
  48.   Result := Self.TryAdd(NewEntry.ID, NewEntry);
  49. end;
  50.  
  51. procedure TDataStore<T>.DeleteEntry(const ID: Integer);
  52. begin
  53.   Remove(ID);
  54. end;
  55.  
  56. constructor TDataStore<T>.Create;
  57. begin
  58.   inherited Create([doOwnsValues]);
  59. end;
  60.  
  61. procedure TDataStore<T>.DeleteEntry(Entry: TDataStoreEntry);
  62. begin
  63.   Remove(Entry.ID);
  64. end;
  65.  
  66. destructor TDataStore<T>.Destroy;
  67. begin
  68.  
  69.   inherited;
  70. end;
  71.  
  72. function TDataStore<T>.GetEntry(ID: Integer): T;
  73. begin
  74.   if ContainsKey(ID) then
  75.     Result := Items[ID]
  76.   else
  77.     Result := NULL_DATASTORE_ENTRY;    // <---- [dcc32 Error] CommonDataStore.pas(77): E2010 Incompatible types: 'T' and 'TDataStoreEntry'
  78. end;
  79.  
  80. { TDataStoreEntry }
  81.  
  82. function TDataStoreEntry.Delete: Boolean;
  83. begin
  84.  
  85. end;
  86.  
  87. function TDataStoreEntry.GetDataStore: TDataStore<TDataStoreEntry>;
  88. begin
  89.  
  90. end;
  91.  
  92. function TDataStoreEntry.GetIsNull: Boolean;
  93. begin
  94.   Result := False;
  95. end;
  96.  
  97. type
  98.   TNullDataStoreEntry = class(TDataStoreEntry)
  99.   protected
  100.     function GetIsNull: Boolean; override;
  101.   end;
  102.  
  103. { TNullDataStoreEntry }
  104.  
  105. function TNullDataStoreEntry.GetIsNull: Boolean;
  106. begin
  107.   Result := True;
  108. end;
  109.  
  110. initialization
  111.   NULL_DATASTORE_ENTRY := TNullDataStoreEntry.Create;
  112.  
  113. finalization
  114.   NULL_DATASTORE_ENTRY.Free;
  115.  
  116. end.
  117.  
nil

Offline Radek Červinka

  • Administrátoři
  • Padawan
  • *****
  • Příspěvků: 3298
  • Karma: 112
    • Verze Delphi: D2007, DXE + 2 poslední
    • O Delphi v češtině
Re:Generika a hláška "incompatible types"
« Odpověď #1 kdy: 12-10-2022, 14:40:03 »
Zkus

Kód: Delphi [Vybrat]
  1.    
  2. function TDataStore<T>.GetEntry(ID: Integer): T;
  3.     begin
  4.       if ContainsKey(ID) then
  5.         Result := Items[ID]
  6.       else
  7.         Result := T(NULL_DATASTORE_ENTRY);
  8.     end;
  9.  
Embarcadero MVP - Czech republic

Offline Morrison

  • Hrdina
  • ****
  • Příspěvků: 414
  • Karma: 12
    • Verze Delphi: D5, XE2, 10.4.2, D11
Re:Generika a hláška "incompatible types"
« Odpověď #2 kdy: 12-10-2022, 20:24:10 »
Díky, Radku. Explicitní přetypování samozřejmě funguje. Nicméně si myslím, že by to měl kompilátor pochroupat i bez toho. Proto jsem tam to typové vymezení <T: TDataStoreEntry> dával.
nil