Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2010-08

How to set a type as a variable?

6 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Frank Church

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)
var
  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?

Thanks

Frank Church ======================= http://devblog.brahmancreations.com
Anthony Walter
to Frank Church

Like so:

procedure Test;
var
  SomeType: TClass;
  SomeObject: TObject;
begin
  SomeType := TStringList;
  SomeObject := SomeType.Create;
  (SomeObject as TStrings).Add('Hello World');
  ShowMessage((SomeObject as TStrings)[0]);
  SomeObject.Free;
end;
Frank Church wrote 32 lines
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) var 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? Thanks -- Frank Church ======================= http://devblog.brahmancreations.com _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
leledumbo
to Anthony Walter

It can be implemented with RTTI or variants perhaps.

Anthony Walter-3 wrote 57 lines
Like so: procedure Test; var SomeType: TClass; SomeObject: TObject; begin SomeType := TStringList; SomeObject := SomeType.Create; (SomeObject as TStrings).Add('Hello World'); ShowMessage((SomeObject as TStrings)[0]); SomeObject.Free; end; On Mon, Aug 23, 2010 at 7:05 PM, Frank Church wrote: > > 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) > var > 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? > > Thanks > > -- > Frank Church > > ======================= > http://devblog.brahmancreations.com > > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-pascal > _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
View this message in context: http://old.nabble.com/How-to-set-a-type-as-a-variable--tp29517255p29518818.html Sent from the Free Pascal - General mailing list .
leledumbo
to Anthony Walter: Like so: procedure Test; var SomeType: TClass; SomeObject: TObject; begin SomeType :=…
Anthony Walter-3 wrote 57 lines
Like so: procedure Test; var SomeType: TClass; SomeObject: TObject; begin SomeType := TStringList; SomeObject := SomeType.Create; (SomeObject as TStrings).Add('Hello World'); ShowMessage((SomeObject as TStrings)[0]); SomeObject.Free; end; On Mon, Aug 23, 2010 at 7:05 PM, Frank Church wrote: > > 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) > var > 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? > > Thanks > > -- > Frank Church > > ======================= > http://devblog.brahmancreations.com > > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-pascal > _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
View this message in context: http://old.nabble.com/How-to-set-a-type-as-a-variable--tp29517255p29518821.html Sent from the Free Pascal - General mailing list .
Felipe Monteiro de Carvalho
to Frank Church: I did this kind of thing some time ago, but have forgotten the details. Let us say you…

No, if all your types are classes descending from the same root you can do it is like this:

type
  TBaseType = class;
    procedure somemethod();
  end;
  TExtendedType = class(TBaseType);
  TBaseTypeClass = class of TBaseType;
procedure (value: TBaseType; type_: TBaseTypeClass)
begin
  value := type_.Create();
  value.somemethod();
end;

And you can call your procedure as:

proc(somevar, TExtendedType);

For other types you need to use generics.

Felipe Monteiro de Carvalho
Marco van de Voort
to Frank Church: I did this kind of thing some time ago, but have forgotten the details. Let us say you…

In our previous episode, Frank Church said:

Show quoted text 18 lines
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) var 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.