Re: [fpc-pascal] constructor as procvar

2010-03-22 Thread Michael Van Canneyt
On Sat, 20 Mar 2010, David Emerson wrote: How can I obtain a class type variable from an instance? I want to create a second instance of the same descendant class (via the constructor, which will take some parameters and make the new instance unique) Try using Instance.ClassType. Something l

Re: [fpc-pascal] constructor as procvar

2010-03-20 Thread Juha Manninen
Hi The essential part is to have virtual constructor like in Florian Klaempfl's example code: t_mammal = class public constructor create (color : byte); virtual; end; and then... t_pig = class(t_mammal) public constructor create (color : byte); override; end; t

Re: [fpc-pascal] constructor as procvar

2010-03-20 Thread Andrew Hall
Hi David, If I'm reading your need correctly... function CopyMyMammal(value: T_Mammal; extraParameters): T_Mammal; begin result := T_Mammal_Class(value.ClassType).Create(withExtraParameters); end; Only confusion is you say you cannot make the type conversion? But the above code is correct and

Re: [fpc-pascal] constructor as procvar

2010-03-20 Thread David Emerson
> Tobject.classtype returns TClass (class of TObject) which I first > assumed would work great. However I cannot seem to do a type > conversion from TClass to t_mammal_class (class of t_mammal)! So I am > stuck here. well, I got this working, not sure why I had so much trouble with a simple typ

Re: [fpc-pascal] constructor as procvar

2010-03-20 Thread Marc Weustink
David Emerson wrote: How can I obtain a class type variable from an instance? I want to create a second instance of the same descendant class (via the constructor, which will take some parameters and make the new instance unique) Tobject.classtype returns TClass (class of TObject) which I fir

Re: [fpc-pascal] constructor as procvar

2010-03-20 Thread David Emerson
How can I obtain a class type variable from an instance? I want to create a second instance of the same descendant class (via the constructor, which will take some parameters and make the new instance unique) Tobject.classtype returns TClass (class of TObject) which I first assumed would work

Re: [fpc-pascal] constructor as procvar

2010-03-19 Thread David Emerson
Florian Klaempfl wrote: > Constructor procvars are indeed not supported but the way to achieve > what you want is to use class type variables > > t_mammal_class = class of t_mammal; > >   function find_or_create_animal (color : byte; >     pass_mammal_type : t_mammal_class) : t_mammal; > >  

Re: [fpc-pascal] constructor as procvar

2010-03-19 Thread Florian Klaempfl
> As you can (probably) see, I would like t_barnyard to be able to create > any descendant of t_mammal. In the above example, I want it to create a > brown pig if it doesn't find one, so I tried to pass the t_pig > constructor, along with the brown parameter that said constructor will > require

[fpc-pascal] constructor as procvar

2010-03-19 Thread David Emerson
I'd like to pass a constructor as a procvar. Don't know if this is possible. Here is some silly illustrative code that doesn't work: {$mode objfpc} uses classes, sysutils; type t_mammal = class public constructor create (color : byte); virtual; end; t_pig = class (t_mammal)