Heinrich Ulbricht
Heinrich Ulbricht

Reputation: 10372

Is it possible to derive nested class from generic type provided by outer class?

I'd like to create a nested class which is based on the type provided to the outer class. I need the inner class to extend T by some members:

TOuterClass<T:class> = class
  type
    TNestedClass = class(T)
      MoreData:Integer;
    end;
end;

The compiler says "No" or more specifically [DCC Error] MyUnit.pas(20): E2021 Class type required at class(T).

Is it somehow possible to achieve what I am trying to do?

Upvotes: 0

Views: 383

Answers (3)

Kenneth Cochran
Kenneth Cochran

Reputation: 12114

No but you can in a derived class that has resolved the type of T:

TOuterClass<T:class> = class
  //Data
end;

TDerived = class(TOuterClass<TObject>)
  type
    TNestedClass = class(TObject)
      MoreData:Integer;
    end;
end;

Upvotes: 0

Mason Wheeler
Mason Wheeler

Reputation: 84650

Not yet. It probably should, but the compiler doesn't really think through all the ramifications of generic constraints yet. You should add this into QC as a feature request.

Upvotes: 2

Barry Kelly
Barry Kelly

Reputation: 42182

No, that's not possible.

Upvotes: 6

Related Questions