[fpc-pascal] How to set a type as a variable?
Marco van de Voort
marcov at stack.nl
Tue Aug 24 08:15:43 CEST 2010
In our previous episode, Frank Church said:
> I did this kind of thing some time ago, but have forgotten the details.
> Let us say you have something like TBaseType(Variable).Method, you want to
> let TBaseType be a variable so you can have some code like this.
> procedure (value:someType)
> variableType: TClass; //not quite sure here
> variableType := TypeOf(value);
> then further on you execute something like
> variableType(Variable).Method, rather than TBaseType(Variable).Method
> because you can't hard code TBaseType because can't tell what it will be at
> runtime. I remember doing something like that with Delphi of old, probably
> Delphi 3 or Delphi 7
> Can someone help me with this, with some use cases as well?
There is typeinfo in Delphi/fpc, but that is for RTTI, and quite different
The above is a bit strange in Pascals with unit system, since it potentially
creates a var of a type that might not be in the scope.
So I think it is not even possible to implement this in a straight way.
More information about the fpc-pascal