Delphi: why complain about the lack of a method?

I want to use IEnumerator<T> instead of IEnumerator for the list I am creating. I tried the following

 IMyList = interface(IEnumerator<TElement>) procedure Add(Element: TElement); end; TMyList = class(TInterfacedObject, IMyList ) private function GetCurrent: TElement; function MoveNext: Boolean; procedure Reset; public property Current: TElement read GetCurrent; procedure Add(Element: TElement); end; 

but, to my surprise, they tell me that TMyList does not implement GetCurrent . Why does the compiler tell me that GetCurrent absent when obviously not? ( GetCurrent implemented for recording, omitted here for brevity). Thanks!

+6
source share
1 answer

IEnumerator<T> inherits the IEnumerator interface. Both of them have the GetCurrent() method, and one of them is the usual method, and the second is the generics T method;

therefore, in your class you must implement both of them getCurrent():TObject (from IEnumerator ) and getCurrent():T (from IEnumerator<T> );

one small problem is that both methods have the same parameters, and you cannot just declare them both. therefore you should use aliases:

  function getCurrentItem() : TElement; //actual method for IEnumerator<T> function GetCurrent():TObject; //method for IEnumerator function IMyList.GetCurrent = getCurrentItem; //alias 

Take a look at the Method Permission Suggestion in docwiki http://docwiki.embarcadero.com/RADStudio/en/Implementing_Interfaces

therefore, in your case, the code should look like (I designated all the Abstract methods):

 TElement = class(TObject) end; IMyList = interface(IEnumerator<TElement>) procedure Add(Element: TElement); end; TMyList = class(TInterfacedObject, IMyList ) private function getCurrentItem() : TElement; virtual; abstract; function IMyList.GetCurrent = getCurrentItem; function GetCurrent():TObject; virtual; abstract; function MoveNext(): Boolean; virtual; abstract; procedure Reset(); virtual; abstract; public property Current: TElement read GetCurrentItem; procedure Add(Element: TElement); virtual; abstract; end; 
+8
source

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


All Articles