Clone descendant object in parent abstract class lazarus / 21-General ---------------------------------------------------------------------- _N_ · 2021-03-11 07:26 Hello all. I have an abstract parent class that implements some common methods including constructor that relies on virtual abstract methods. Descendant classes (I have a dozen of them) have implementations of the abstract methods, but no constructors: they inherit it. Here everything works fine, new instances of any descendant class can be created. The problem comes when I need to clone an object. I want to have a single implementation of Clone method in parent class as it is done with constructor Create. I realize that I have to cast parent to specific descendant on clone, but it is ok for me (if you know better solution, please share it). However, when I try to invoke constructor inside Clone method I got the following error: "Invalid assignment, procedures return no value" Why does this happen? As I understand Clone method is invoked on a descendant class object which has inherited the constructor that relies on virtual methods implemented in the object class. Should work, but it doesn't. Can someone explain what is wrong here? If there is any other solution to my problem I would be happy to see them. Thank you in advance. Simplified code to reproduce the issue: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Test;uses  Classes, SysUtils;type   { TParent }   TParent = class(TObject)  public    constructor Create(Data: TStringList);    function Clone(): TParent;     procedure Print(); virtual; abstract;  protected    procedure Parse(Data: TStringList); virtual; abstract;  function ExportData(): TStringList; virtual; abstract;  end;   { TChild1 }   TChild1 = class(TParent)  private    FData: Integer;  protected    procedure Parse(Data: TStringList); override;    function ExportData(): TStringList; override;  public    property Data: Integer read FData write FData;     procedure Print(); override;  end;   { TChild2 }   TChild2 = class(TParent)  private    FData: Real;  protected    procedure Parse(Data: TStringList); override;    function ExportData(): TStringList; override;  public    property Data: Real read FData write FData;     procedure Print(); override;  end; { TParent } constructor TParent.Create(Data: TStringList);begin  Parse(Data);end; function TParent.Clone(): TParent;var  List: TStringList;begin  List := ExportData();  Result := Create(List); // <-- Error: Invalid assignment, procedures return no value  List.Free();end; { TChild1 } procedure TChild1.Parse(Data: TStringList);begin  FData := StrToInt(Data[0]);end; function TChild1.ExportData(): TStringList;var  List: TStringList;begin  List := TStringList.Create();  List.Add(IntToStr(FData));  Result := List;end; procedure TChild1.Print();begin  WriteLn('Child1:', FData);end; { TChild2 } procedure TChild2.Parse(Data: TStringList);begin  FData := StrToFloat(Data[0]);end; function TChild2.ExportData(): TStringList;var  List: TStringList;begin  List := TStringList.Create();  List.Add(FloatToStr(FData));  Result := List;end; procedure TChild2.Print();begin  WriteLn('Child2:', FData);end; var  o1: TChild1;  o2: TChild2;  oCopied: TChild2;  l1, l2: TStringList;begin  l1 := TStringList.Create();  l1.Add('1234');  l2 := TStringList.Create();  l2.Add('12.34');   o1 := TChild1.Create(l1);  o2 := TChild2.Create(l2);   oCopied := TChild2(o2.Clone());  oCopied.Data := 23.45;   o1.Print();  o2.Print();  oCopied.Print();   o1.Free();  o2.Free();  oCopied.Free();  l1.Free();  l2.Free();end. ---------------------------------------------------------------------- howardpc · 2021-03-11 09:23 With a few changes as below your code will compile. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Test; uses  Classes, SysUtils;type   TParentClass = class of TParent;   TParent = class(TObject)  public    constructor Create(Data: TStringList);    function Clone(aClass: TParentClass): TParent;     procedure Print(); virtual; abstract;  protected    procedure Parse(Data: TStringList); virtual;    function ExportData(): TStringList; virtual; abstract;  end;   TChild1 = class(TParent)  private    FData: Integer;  protected    procedure Parse(Data: TStringList); override;    function ExportData(): TStringList; override;  public    property Data: Integer read FData write FData;  procedure Print(); override;  end;   TChild2 = class(TParent)  private    FData: Real;  protected    procedure Parse(Data: TStringList); override;    function ExportData(): TStringList; override;  public    property Data: Real read FData write FData;     procedure Print(); override;  end; constructor TParent.Create(Data: TStringList);begin  Parse(Data);end; function TParent.Clone(aClass: TParentClass): TParent;var  List: TStringList;begin  List := ExportData();  Result := aClass.Create(List);  List.Free();end; procedure TParent.Parse(Data: TStringList);begin  //end; procedure TChild1.Parse(Data: TStringList);begin  FData := StrToInt(Data[0]);end; function TChild1.ExportData(): TStringList;var  List: TStringList;begin  List := TStringList.Create();  List.Add(IntToStr(FData));  Result := List;end; procedure TChild1.Print();begin  WriteLn('Child1:', FData:5);end; procedure TChild2.Parse(Data: TStringList);begin  FData := StrToFloat(Data[0]);end; function TChild2.ExportData(): TStringList;var  List: TStringList;begin  List := TStringList.Create();  List.Add(FloatToStr(FData));  Result := List;end; procedure TChild2.Print();begin  WriteLn('Child2:', FData:6:2);end; var  o1: TChild1;  o2: TChild2;  oCopied: TChild2;  l1, l2: TStringList; begin  l1 := TStringList.Create();  l1.Add('1234');  l2 := TStringList.Create();  l2.Add('12.34');   o1 := TChild1.Create(l1);  o2 := TChild2.Create(l2);   oCopied := TChild2(o2.Clone(TChild2));  oCopied.Data := 23.45;   o1.Print();  o2.Print();  oCopied.Print();   o1.Free();  o2.Free();  oCopied.Free();  l1.Free();  l2.Free();end. ---------------------------------------------------------------------- Mr.Madguy · 2021-03-11 11:44 I had this problem too and I had to implement it like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TAbstractClass = class  protected    function CreateSelf:TAbstractClass;virtual;abstract;end; TSomeClass = class  protected    function CreateSelf:TAbstractClass;override;end; function TSomeClass.CreateSelf:TAbstractClass;begin  Result := TSomeClass.Create;end; I don't remember exactly, why I wasn't able to use this solution, but all of a sudden it seems to work: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs; type   { TForm1 }   TForm1 = class(TForm)    procedure FormCreate(Sender: TObject);  private   public   end;   TAbstractClass = class    protected      function GetString:String;virtual;abstract;    public      constructor Create(AString:String);virtual;abstract;      function Clone:TAbstractClass;      property SomeString:String read GetString;  end;   TSomeClass = class(TAbstractClass)    protected      FString:String;      function GetString:String;override;    public    constructor Create(AString:String);override;  end;   TMyClass = class of TAbstractClass; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } function TAbstractClass.Clone:TAbstractClass;begin  Result := TMyClass(ClassType).Create(SomeString);end; constructor TSomeClass.Create(AString:String);begin  FString := AString;end; function TSomeClass.GetString:String;begin  Result := FString;end; procedure TForm1.FormCreate(Sender: TObject);  var A, B:TAbstractClass;begin  A := TSomeClass.Create('Hello!');  B := A.Clone;  ShowMessage(A.SomeString);  ShowMessage(B.SomeString);  B.Free;  A.Free;end; end. ---------------------------------------------------------------------- engkin · 2021-03-11 12:29 A class instance has its own class type: ClassType ---------------------------------------------------------------------- ASerge · 2021-03-11 15:47 Due to the fact that the class knows its type, you can simplify the Clone function to the following: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TParent.Clone: TParent;begin  Result := TParentClass(ClassType).Create(ExportData);end;//...CCopied := TChild2(C2.Clone);Example: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$MODE OBJFPC}{$APPTYPE CONSOLE}{$LONGSTRINGS ON}uses SysUtils; type  TParent = class(TObject)  protected    function ExportData: string; virtual; abstract;    procedure Parse(const Data: string); virtual; abstract;  public    constructor Create(const Data: string);    function Clone: TParent;    procedure Print; virtual; abstract;  end;   TChild1 = class(TParent)  private    FData: Integer;  protected    function ExportData: string; override;    procedure Parse(const Data: string); override;  public    property Data: Integer read FData write FData;  procedure Print; override;  end;   TChild2 = class(TParent)  private  FData: ValReal;  protected    procedure Parse(const Data: string); override;    function ExportData: string; override;  public    property Data: ValReal read FData write FData;    procedure Print; override;  end; constructor TParent.Create(const Data: string);begin  Parse(Data);end; function TParent.Clone: TParent;type  TParentClass = class of TParent;begin  Result := TParentClass(ClassType).Create(ExportData);end; procedure TChild1.Parse(const Data: string);begin  FData := StrToInt(Data);end; function TChild1.ExportData: string;begin  Result := IntToStr(FData);end; procedure TChild1.Print;begin  WriteLn('Child1:', FData:5);end; procedure TChild2.Parse(const Data: string);begin  FData := StrToFloat(Data);end; function TChild2.ExportData: string;begin  Result := FloatToStr(FData);end; procedure TChild2.Print;begin  WriteLn('Child2:', FData:6:2);end; procedure Main;var  C1: TChild1;  C2: TChild2;  CCopied: TChild2;begin  C1 := TChild1.Create('1234');  try    C2 := TChild2.Create('12.34');    try      CCopied := C2.Clone as TChild2;      try        CCopied.Data := 56.78;        C1.Print;      C2.Print;        CCopied.Print;      finally        CCopied.Free;      end;    finally      C2.Free;    end;  finally    C1.Free;  end;end; begin  Main;  Readln;end. ---------------------------------------------------------------------- _N_ · 2021-03-11 17:04 Thank you guys! It works now and I don't have to copy the code to all descendant classes! ---------------------------------------------------------------------- marcov · 2021-03-11 17:44 If you don't pass data in the consstructor, but use e.g. assign(), you can further generalize the concept, see e.g. my answer on stack overflow about object cloning ---------------------------------------------------------------------- Mr.Madguy · 2021-03-12 05:44 Now I know, why I couldn't use this solution. It doesn't work for generics. This one doesn't work: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   TMyClass = class of TMyObject; ---------------------------------------------------------------------- Leledumbo · 2021-03-18 11:33 Quote from: Mr.Madguy on March 12, 2021, 06:44:09 amNow I know, why I couldn't use this solution. It doesn't work for generics. This one doesn't work: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   TMyClass = class of TMyObject;  Unless it's declared in public type section of TMyObject, like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$mode objfpc} type  generic TMyObject = class  public type    TMyClass = class of TMyObject;  public    function DoIt: TMyObject;  end;   generic TMyDescObj = class(specialize TMyObject)  end;   TMyObjectInt = specialize TMyObject;   TMyDescObjInt = specialize TMyDescObj; function TMyObject.DoIt: TMyObject;begin  WriteLn('TMyObject');  Result := Self;end; var  a: TMyObjectInt.TMyClass;begin  a := TMyDescObjInt;  a.Create.DoIt.Free;end. ---------------------------------------------------------------------- Mr.Madguy · 2021-03-18 11:52 Quote from: Leledumbo on March 18, 2021, 12:33:42 pmUnless it's declared in public type section of TMyObject, like this: Ok, I'll check it, when I'll have time. May be there was another reason, why I couldn't do it. I guess, there are not so many places left in my program, where it's needed, after it was overhauled.