Cannot return generic derived class
Original Reporter info from Mantis: sysrpl
-
Reporter name: Anthony Walter
Original Reporter info from Mantis: sysrpl
- Reporter name: Anthony Walter
Description:
It would seem there is a problem with the compiler when returning a generic class if the class is not specifically referenced elsewhere. See example in steps to reproduce as well as the attached example.
When the error occurs you will see a message like "Error: Forward declaration not solved"
The effect of this problem is that it's not current possible to derive from generic classes unless the derived class has a variable (reference) of it's specific type placed somewhere in the open.
I've encountered a few places where this problem creeps up. One place is in my generic TEnumerator&LtPos;T> class where I attempt to return a TListEnumerator&LtPos;T> from a overridden virtual "GetEnumerator: TEnumerator&LtPos;T>" method. The code will not compile and I am left with the "Error: Forward declaration not solved" message referring to my TListEnumerator&LtPos;T> methods.
Steps to reproduce:
Example:
type TA<T> = class
public
function Foo(Value: T): T; virtual; abstract;
end;
type TB<T> = class(TA<T>)
public
// Error: Forward declaration not solved
function Foo(Value: T): T; override;
end;
function TB<T>.Foo(Value: T): T;
begin
Result := Value;
end;
type TC<T> = class
public
function GetA: TA<T>;
end;
function TC<T>.GetA: TA<T>;
begin
Result := TB<T>.Create;
end;
procedure Test;
var
C: TC<String>;
A: TA<String>;
// Uncomment this one line below and the error goes away
// B: TB<String>;
begin
C := TC<String>.Create;
A := C.GetA;
A.Free;
C.Free;
end;
Additional information:
The problem can be demonstrated to not be "real" by adding a variable of the derived type somewhere. See "uncomment" section in steps to reproduce.
Mantis conversion info:
- Mantis ID: 22019
- OS: Ubuntu
- OS Build: 12.04
- Build: trunk
- Platform: Linux
- Version: 2.7.1
- Monitored by: » arjument (Michael P. Qarakulow), » PeterJanRoes (Peter-Jan Roes)