The below code does not work.
program Project7;
{$APPTYPE CONSOLE}
{$R *.res}
uses System.SysUtils;
type
I1 = interface
['{B4BF44AD-23A9-4F42-BA2B-6E137E22344E}']
procedure Test1;
end;
I2 = interface(I1)
['{AAAAAAAA-23A9-4F42-BA2B-BBBBBBBBBBBB}']
procedure Test2;
end;
T12 = class(TInterfacedObject, I2)
public
procedure Test1;
procedure Test2;
public
function MeAsI1: I1;
function MeASI2: I2;
end;
function T12.MeAsI1: I1;
begin
Result:= (self as I1);
end;
function T12.MeASI2: I2;
begin
Result:= (self as I2);
end;
procedure T12.Test1;
begin
Writeln('T12: test1 from interface i1');
end;
procedure T12.Test2;
begin
Writeln('T12: test2 from interface i2');
end;
Implemenation:
var
MyClass: T12;
AI1: I1;
AI2: I2;
begin
MyClass:= T12.Create;
AI2:= MyClass.MeAsI2;
AI2.Test2;
Readln;
AI1:= MyClass.MeAsI1; //<< Exception interface not supported
AI1.Test1;
Readln;
end.
It gives an Exception: interface not supported
.
It looks like interface inheritance
does not really work like class inheritance
.
If I add the interface I1
to class T12
it does work, but this gets a bit silly when a class implements many interfaces.
Is there a way to only declare I2
and still be able to return a reference to I1 from inside the class?
I'm using Delphi XE6, but I'm sure the error is the same in Delphi 3.
EDIT
A use case for this would be:
IReadOnly = interface
function GetSomething: integer;
...
IRWIntf = interface(IReadOnly)
procedure SetSomething(value: integer);
....