[fpc-devel] [Feature Request] get nested type from a generic class

ZAN DoYe 1123monkey at gmail.com
Fri Sep 21 11:05:27 CEST 2012


On 2012-09-21 15:49, Sven Barth wrote:
> If you already expect that "C" is a "tLess<Something>" then you should write it the following way:
>
> === source begin ===
>
> // tLess<T> as in your example
>
> generic tCompOps<T> = class
>   public type
>     TLessT = specialize tLess<T>;
>   public
>     // the remaining methods are like in your example with the exception that in the implementation you don't use type "C", but "TLessT"
> end;
>
> === source end ===
Thanks for your suggestion.
But what *tCompOps<C>* expects is a user-defined class which has a public function `lessThan(const a, b: keyType): boolean', a public type `keyType'.
These tLess(_obj|_rec)? classes are just helper class to wrap some simple specific type.

Users may wanna compare a record contains strings. In this case, binaryCompare is different from a user-defined class that compare two record with `if rec1.str < rec2.str'. As the previous method essentially compare two pointers, not two strings.

Though we can manually specify the keyType, i.e. define tCompOps as generic tCompOps<T, C>. But define all thing in one class is more convenient(for later use, pass it to other generator, etc.).




More information about the fpc-devel mailing list