1
type
  TObjA = class
    a: string;
  end;
type
  worker<T> = interface
    function step1(v: integer): T;
    function step2(s: string): T;
  end;

type
  ImplA<T> = class(TInterfacedObject, worker<T>)
    function step1(v: integer): T;
    function step2(s: string): T; virtual; abstract;
  end;

type
  ImplB = class(ImplA<TObjA>)
    function step2(s: string): TObjA;
  end;
implementation


function ImplA<T>.step1(v: integer): T;
begin
  result := step2(IntToStr(v));
end;

function ImplB.step2(s: string): TObjA;
var
  r: TObjA;
begin
  r := TObjA.Create;
  r.a := 'step2 ' + s;
  result := r;
end;

I am trying to build a functionality according to this structure. I know it works in java, but currently I am working in delphi 2010. I get an abstract error when calling ImplB.step1(1) How do I fix this?

0

1 Answer 1

5

You get the error as you do not declare function step2(s: string): TObjA; as an override.

So in

function ImplA<T>.step1(v: integer): T;
begin
  result := step2(IntToStr(v));
end;

it is calling step2 from ImplA not ImplB as you are expecting it to

Your also changing the return type from a generic object to TObjA, the compiler may not like that, but I don't have a copy of Delphi that supports generics to hand to test.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, that was it exactly. All I had to do was add the override keyword. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.