[fpc-pascal] Case statement for class introspection

Ryan Joseph genericptr at gmail.com
Sun Jan 16 03:21:05 CET 2022



> On Jan 15, 2022, at 3:24 PM, Michael Van Canneyt via fpc-pascal <fpc-pascal at lists.freepascal.org> wrote:
> 
> I don't see how an inline variable helps with the casting mess. You'll
> always need a cast.
> 
> What I do is Var
>  MyInstance : TObject;
>  MyNeededClass : TMyNeededClass absolute myInstance:

Yes that's the best option.

Anyways, the case statement in FPC allows for strings which basically fold down into an if-else statement for comparing strings and this same logic could be expanded for class types. Seems like a smart extension to add but does the compiler team support this?

Something like this:

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

Converts to:

  if o.ClassType = TObject then
    writeln('TObject')
  else if o.ClassType = TInterfacedObject then
    writeln('TInterfacedObject');

There is a possibility for using "as" operator also though....

Regards,
	Ryan Joseph



More information about the fpc-pascal mailing list