[fpc-pascal] Compatibility of very similar generic types

Sven Barth pascaldragon at googlemail.com
Thu Aug 18 14:44:24 CEST 2016


Am 18.08.2016 10:57 schrieb "Felipe Monteiro de Carvalho" <
felipemonteiro.carvalho at gmail.com>:
>
> Hello,
>
> Consider this:
>
> TSubClass1 = class(TSomeClass)...;
> TSubClass2 = class(TSomeClass)...;
>
> TSubClass1Vector = specialize TVector<TSubClass1>;
> TSubClass2Vector = specialize TVector<TSubClass2>;
> TSomeClassVector = specialize TVector<TSomeClass>;
>
> And I want to write a function like:
>
> procedure WriteVector_To_DM(ADest: TDataManager; ASelf: TSomeClassVector);
> procedure ReadVector_From_DM(AFrom: TDataManager; var ASelf:
TSomeClassVector);

[snip]
>
> Now I understand that some people will scream: DON'T DO THIS.
>
> But my question is: Is there any real reason why I am wrong and will
> fail in some future compiler version or something, or while not
> recommended I am correct that there is no real reason why it could
> possibly fail. Considering my points above?

What you're looking for is a form of type coercion that we don't support
(at least currently; dont know what the future will bring).
Also your assumption is only correct as it is now, however it could be in
the future that the compiler might generate different code for the
different classes, cause of different optimization potential (unlikely, but
I hope you get my point), thus you are relying on implementation details
and thus it will be your own fault if things should stop working in the
future.
One of the cleaner solutions (aside from possible type coercion) is
available on trunk: generic function.
Declare your functions like this:

generic procedure WriteVector_To_DM<T>(ADest: TDataManager; ASelf:
specialize TVector<T>);
generic procedure ReadVector_From_DM<T>(AFrom: TDataManager; var ASelf:
specialize TVector<T>);

and use them like this:

specialize WriteVector_To_DM<TSubClass1>(...);

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


More information about the fpc-pascal mailing list