I am trying to use a type from another type.
Test is working but Test2 raises an error. Why?
In my opinion, even when compiling the Test procedure, an error should occur. Let 's rewrite it in a different way:
{$MODE DELPHI}
{$APPTYPE CONSOLE}
type
TObj<T> = object
procedure Test;
end;
procedure TObj<T>.Test;
begin
WriteLn(SizeOf(T.T2)); // At this point T.T2 is unknown ???
end;
type
TClass = class
type T2 = Integer;
end;
var
V: TObj<TClass>;
begin
V.Test;
ReadLn;
end.
But if we explicitly restrict the template type, then there will be no problems:
{$MODE DELPHI}
{$APPTYPE CONSOLE}
type
TClass = class
type T2 = Integer;
end;
TObj<T: TClass> = object
procedure Test;
end;
procedure TObj<T>.Test;
var
A: T.T2;
begin
WriteLn(SizeOf(A));
end;
var
V: TObj<TClass>;
begin
V.Test;
ReadLn;
end.