|
Generics with Delphi 2009 Win32
With, as a bonus, anonymous routines and routine references
Date de publication : November 13th, 2008 IX. RTTI and generics IX-A. Changes to the pseudo-routine TypeInfo IX-A-1. A more general TypeInfo function IX-B. Do generic types have RTTI? IX. RTTI and generics
In this last chapter, I am going to give some information about what generics changed to RTTI. If you never play with
RTTI, you should skip this entire chapter. It is not intended at all to be an introduction to RTTI.
IX-A. Changes to the pseudo-routine TypeInfo
RTTI always begin with the pseudo-routine TypeInfo. You may know that certain types cannot be applied to this
pseudo-routine; for example, pointer types. And that, because of that, it never returns nil.
So, may we call TypeInfo on a generic type T? The question is pertinent: T might as well be a
pointer type (invalid for TypeInfo) or an integer type (valid).
The answer is yes, one can call TypeInfo on a generic type. But what happens then if T results in a
type that has no RTTI? Well, in this only case, TypeInfo returns nil.
To illustrate the phenomenon, here is a small class method that prints the name and the kind of type, but via
generics:
This method is quite special, in the sense that its real parameter is actually passed as a parameterized type.
Whose execution yields:
IX-A-1. A more general TypeInfo function
I have already regretted that TypeInfo could not be applied to any type, even for a nil reply; maybe you did
as well. Then, here is a small replacement method solving the method.
Which you may use as:
IX-B. Do generic types have RTTI?
There are two questions here: do non instaciated generic types (with a non defined T parameter) have RTTI?
And do instanciated generic types (where T has been replaced with an actual type, like Integer)
have RTTI?
The easiest way to know it is to try ;-) You may observe, with a simple test, that only instanciated generic
types. Actually, that is quite logical, since non instanciated generic types are not actual types, but type
templates, and do not exist once compilation is done.
One can ask himself what name would such a type have. So:
Which yields:
This shows that the name includes, between angle brackets, the fully qualified name of the actual type replacing the
type parameter.
That is all I wanted to say about RTTI and generics. I will not of course speak about other changes brought to RTTI in
Delphi 2009 because of Unicode strings.
Warning: include(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in /home/developpez/www/developpez-com/upload/sjrd/delphi/tutoriel/generics/index.php on line 41 Warning: include(http://sjrd.developpez.com/references.inc): failed to open stream: no suitable wrapper could be found in /home/developpez/www/developpez-com/upload/sjrd/delphi/tutoriel/generics/index.php on line 41 Warning: include(): Failed opening 'http://sjrd.developpez.com/references.inc' for inclusion (include_path='.:/opt/php56/lib/php') in /home/developpez/www/developpez-com/upload/sjrd/delphi/tutoriel/generics/index.php on line 41 |
Copyright © 2008-2009 Sébastien Doeraene. Aucune reproduction, même partielle, ne peut être faite de ce site ni de l'ensemble de son contenu : textes, documents, images, etc. sans l'autorisation expresse de l'auteur. Sinon vous encourez selon la loi jusqu'à trois ans de prison et jusqu'à 300 000 € de dommages et intérêts.