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

Sven Barth pascaldragon at googlemail.com
Fri Sep 21 11:25:36 CEST 2012


Am 21.09.2012 11:05, schrieb ZAN DoYe:
> 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.).

In that case you must always pass the type ("keyType" in your example) 
to tCompOps as well. Anything else does not work. Even generic 
constraints would not work here.

Regards,
Sven




More information about the fpc-devel mailing list