Delphi generic nested classes
Posted by jpluimers on 2019/08/21
Reminder to Self: do not nest T
generic types as you’re in for a surprise.
Source: [WayBack] Delphi generic nested classes
Via: [WayBack] An interesting question over on SO relating to nested generic classes… – David Heffernan – Google+
The surprise:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type | |
TClassC<T> = class | |
private | |
type | |
TClassD<T> = class | |
private | |
x: T; | |
end; | |
end; | |
var | |
obj: TClassC<Integer>.TClassD<string>; |
what type would you expect obj.x to be? Integer or string?
The compiler hint:
[dcc32 Hint]: H2509 Identifier 'T' conflicts with type parameters of container type
–jeroen
Sebastian Jänicke said
I did not try it yet, but I would expect it to be of type Integer. This would be the only logical way as T is already defined when you get to the inner class. So there should be no way to redeclare this type. I would be very surprised, if this would be different.
Sebastian Jänicke said
I would have expected a compiler error to be honest (redeclared type or something like this).