[fpc-pascal] How to use generics and meta class?

Sven Barth pascaldragon at googlemail.com
Fri Feb 28 12:57:32 CET 2014


Am 28.02.2014 12:07 schrieb "Joao Morais" <l at joaomorais.com.br>:
>
> Em 28/02/14 03:44, Sven Barth escreveu:
>
>>
>> === code end ===
>>
>> Currently this will print:
>>
>> === output begin ===
>>
>> Bar
>>
>> === output end ===
>>
>> If the compiler would now naively generate an implementation of
TMyGeneric with TMyType then the result would be:
>>
>> === output begin ===
>>
>> Foo
>>
>> === output end ===
>>
>> This must not be. So the compiler - while it might generate an implicit
TMyType implementation - must not use that implicit implemenation if the
type that is specialized with implements non virtual methods itself. This
is one of the restrictions.
>
>
> Ah, now I see. "TGeneric<T: TType>" is quite different from "var T:
TType". Afaics the former is a placeholder for another type, the latter is
a placeholder for an implementation. Thanks for sharing.

Yes, that's an important difference :)

>
>
>>>> Addendum: this does not exclude the compiler from trying to optimize
>>>> this if the type parameter is indeed a class type. But this will be
>>>> transparent for the user and will have a few restrictions. It does
>>>> however not change that generics aren't full types.
>>>
>>>
>>> :/
>>>
>>> Maybe in the near (or not so near) future, these two approaches
>>> (templates and generics like in Java) may live together? I mean: is this
>>> technically possible?
>>
>>
>> FPC/Delphi generics are more templates than generics. Generics are not
full types. This will not change.
>
>
> Got the point. Anyway current (2.6) generics meets about 80% of my needs
and 2.8 sounds to meet about 98% =). Thanks for the really great work with
generics!

You're welcome. I still have enough work left though ;)

Regards,
Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20140228/9d3ac484/attachment.html>


More information about the fpc-pascal mailing list