[fpc-pascal] Case statement for class introspection

Sven Barth pascaldragon at googlemail.com
Mon Jan 17 07:55:24 CET 2022

Ryan Joseph via fpc-pascal <fpc-pascal at lists.freepascal.org> schrieb am
Mo., 17. Jan. 2022, 02:38:

> > On Jan 16, 2022, at 11:15 PM, Sven Barth <pascaldragon at googlemail.com>
> wrote:
> >
> > The class type already is a unique "ID" for each class type when doing
> an equal comparison. You can essentially take the address of the VMT as the
> constant values that the loaded value is compared against.
> Does that look something like this then? and if so, this is better then
> doing the if-statement block, like string case statements?

At least the compiler would be able to optimize it better.

>   case PtrUInt(o.ClassType.ClassInfo) of
>     PtrUInt(TObject.ClassInfo): writeln('TObject');
>   end;

No, that is the RTTI. While that would work as well that is an additional
indirection. So it would be more like:

case PtrUInt(o.ClassType) of
    PtrUInt(TObject): writeln('TObject');

> Question then is how you get the VMT address as a constant at compile time.

I'll need to get back to you with that.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20220117/b70ef6a5/attachment-0001.htm>

More information about the fpc-pascal mailing list