Cross reference class types between units lazarus / 62-General ---------------------------------------------------------------------- jollytall · 2022-03-08 09:39 I can easily make: 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 type  tChild = class;  tChildren = array of tChild;  tMother = class    Children : tChildren;    end;  tChild = class    Mother : tMother;    end; But how can I make it, if I want to put tChild/tChildren and tMother into different units? As both would be in the interface section, the uses reference should also be there, but it is a circular reference then. ---------------------------------------------------------------------- howardpc · 2022-03-08 11:06 The simplest approach is not to put such declarations in different units, but place the inter-dependent declarations together in a unit designed to collect such class definitions together. Then use the 'collecting' unit wherever it is needed in your other units. ---------------------------------------------------------------------- Zvoni · 2022-03-08 11:12 Maybe include-files? ---------------------------------------------------------------------- jollytall · 2022-03-08 13:49 @howardpc This is what I do. Actually I have always done so, but now in a complex system I thought to be more "elegant" and give every class its own unit. Apparently it was not a good idea. @Zvoni I do not see, how that would solve anything? Include files are sort-of saving typing, i.e. it does not need to be typed (potentially with errors) twice. How would it solve the circular reference issue? ---------------------------------------------------------------------- Zvoni · 2022-03-09 09:58 it compiles...... No idea if it works... 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 child;{$mode ObjFPC}{$H+}interfaceuses Classes, SysUtils;Type  TMother = TClass;  { TChild }  TChild = Class    Public      Mother:TMother;    Constructor Create(AMother:TMother);  end;  TChildren = Array of TChild;implementationUses mother;{ TChild }constructor TChild.Create(AMother: TMother);begin  Mother:=AMother;end;end. 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 mother;{$mode ObjFPC}{$H+}interfaceuses Classes, SysUtils, child;Type  TMother = Class    Public      Children:TChildren;  end;implementationend. 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 project1;uses classes, mother, child;beginend. ---------------------------------------------------------------------- alpine · 2022-03-09 10:39 Quote from: jollytall on March 08, 2022, 10:39:11 amI can easily make: 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 type  tChild = class;  tChildren = array of tChild;  tMother = class    Children : tChildren;    end;  tChild = class    Mother : tMother;    end; But how can I make it, if I want to put tChild/tChildren and tMother into different units? As both would be in the interface section, the uses reference should also be there, but it is a circular reference then. Should you separate mothers from their children? From genealogy POW, one can be both parent and child, so declaring it as a tMother you can't add it later into somebody's children. What if you just add a common ancestor tPerson as it should be? PS. Sorry if I got it wrong and it is just for resolving a circular dependencies into separate units. ---------------------------------------------------------------------- Zvoni · 2022-03-09 10:47 Quote from: y.ivanov on March 09, 2022, 11:39:07 amQuote from: jollytall on March 08, 2022, 10:39:11 amI can easily make: 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 type  tChild = class;  tChildren = array of tChild;  tMother = class    Children : tChildren;    end;  tChild = class    Mother : tMother;    end; But how can I make it, if I want to put tChild/tChildren and tMother into different units? As both would be in the interface section, the uses reference should also be there, but it is a circular reference then. Should you separate mothers from their children? From genealogy POW, one can be both parent and child, so declaring it as a tMother you can't add it later into somebody's children. What if you just add a common ancestor tPerson as it should be? PS. Sorry if I got it wrong and it is just for resolving a circular dependencies into separate units. I think it's more like AControl.Parent = AForm AForm.ArrayOfControls = SomeControls Thoug, i accept that AForm has a Parent, too.... btw: I agree, that both classes should be derived from common ancestor, as pointed out TPerson ---------------------------------------------------------------------- jollytall · 2022-03-09 11:30 The problem, as I see, is that types won't be the same. I guess in Zvoni's three file model, the "uses mother" in line 15 of the first file does not make TMother in that file the same as TMother in the other file. If you insert a line 20 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 if assigned(Mother.Children) then beep;it will not compile as Mother (type tMother = TClass from unit child) has no field called Children (what is in unit mother TMother). Due to the unlucky choice of names (mother is a unit and a field at the same time) it cannot even be typecasted. Changing the field name from Mother to MotherField, it can be made 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 constructor TChild.Create(AMother: TMother);begin  MotherField:=AMother;  if assigned(mother.TMother(MotherField).Children) then beep;end; but it kills the beauty of object oriented programming, if class instances (actually pointers) has to be explicitly typecasted every time one wants to use it. Basically it is the same problem with the common ancestor model as well. If "Thouse" and "Troom" both come from a type "Tlivingarea", I still cannot use numberofrooms field of an locatedinhouse : Tlivingarea field of a Troom instance. Again, it would require explicit typecast. So, I dropped the idea to "separate mother and child" and so it works. Nonetheless, I came across another problem, where even the shared ancestor does not help, if I want to collect some instances in a list, but that is another topic. See https://forum.lazarus.freepascal.org/index.php/topic,58611.0.html ---------------------------------------------------------------------- PascalDragon · 2022-03-09 12:09 Quote from: Zvoni on March 09, 2022, 10:58:46 amit compiles...... No idea if it works... TClass and TObject are two different things, so you're making things even worse than if you'd just use TObject. Also child.TMother will be different from mother.TMother and you'll always have to cast to the latter if you want to access a member of that class. ---------------------------------------------------------------------- alpine · 2022-03-09 12:18 Quote from: jollytall on March 09, 2022, 12:30:31 pm*snip* Basically it is the same problem with the common ancestor model as well. If "Thouse" and "Troom" both come from a type "Tlivingarea", I still cannot use numberofrooms field of an locatedinhouse : Tlivingarea field of a Troom instance. Again, it would require explicit typecast. So, I dropped the idea to "separate mother and child" and so it works. Nonetheless, I came across another problem, where even the shared ancestor does not help, if I want to collect some instances in a list, but that is another topic. See https://forum.lazarus.freepascal.org/index.php/topic,58611.0.html It is quite trivial, you should generalize: 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 type  TPerson = class    function HasChildren: Boolean; virtual; // returns False by default  end;   TChild = class(TPerson)    Mother: TPerson;  end;  TChildren = array of TPerson;   TMother = class(TPerson)    Children: TChildren;    function HasChildren: Boolean; override; // returns Assigned(Children)  end; Where is the problem? ---------------------------------------------------------------------- howardpc · 2022-03-09 15:24 An 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 program classHierarchy; {$mode objfpc}{$H+} type   TPerson = class;   TPersonArray = array of TPerson;   TPerson = class(TObject)  private  fChildren: TPersonArray;    fFather: TPerson;    fFirstName: String;    fMother: TPerson;    fSurname: String;    function GetChildCount: Integer;    function GetChildrensNames: String;    function GetFullname: String;  public    constructor Create(const a1stname, aSurname: String; aChildren: TPersonArray=Nil);    function HasChildren: Boolean;    function HasParent: Boolean;    procedure ReportAncestry;    property ChildCount: Integer read GetChildCount;  property Children: TPersonArray read fChildren;    property ChildrensNames: String read GetChildrensNames;    property Father: TPerson read fFather write fFather;    property FirstName: String read fFirstName;    property Fullname: String read GetFullname;    property Mother: TPerson read fMother write fMother;    property Surname: String read fSurname;  end; constructor TPerson.Create(const a1stname, aSurname: String; aChildren: TPersonArray);begin  fFirstName := a1stname;  fSurname := aSurname;  fChildren := aChildren;end; function TPerson.GetChildCount: Integer;begin  Exit(Length(fChildren));end; function TPerson.GetChildrensNames: String;var  p: TPerson;begin  Result := '';  if HasChildren then    begin      for p in Children do        Result := Result + p.Fullname + ', ';      Delete(Result, Length(Result)-1, 2);    end  else Exit('(no children)');end; function TPerson.GetFullname: String;begin  Exit(FirstName + ' ' + Surname);end; function TPerson.HasChildren: Boolean;begin  Exit(ChildCount > 0);end; function TPerson.HasParent: Boolean;begin  Exit(Assigned(fMother) or Assigned(fFather));end; procedure TPerson.ReportAncestry;begin  Write(Fullname,':');  if Assigned(Mother) and Mother.HasParent then  begin      if Assigned(Mother.Mother) then Write(' Grandmother: ',Mother.Mother.Fullname);      if Assigned(Mother.Father) then Write(' Grandfather: ',Mother.Father.Fullname);    end  else Write(' (no maternal grandparents)');  if Assigned(Father) and Father.HasParent then    begin      if Assigned(Father.Mother) then Write(' Grandmother: ',Father.Mother.Fullname);      if Assigned(Father.Father) then Write(' Grandfather: ',Father.Father.Fullname);    end  else Write(' (no paternal grandparents)');  if Assigned(Mother) then    Write(' Mother: ',Mother.Fullname)  else Write(' (no mother)');  if Assigned(Father) then    Write(' Father: ',Father.Fullname)  else Write(' (no father)');  WriteLn(' Children: ',GetChildrensNames);end; var  grandpa, grandma, mum, dad, mary, trev: TPerson; begin  mary := TPerson.Create('Mary','Blogs');  trev := TPerson.Create('Trev','Blogs');  mum := TPerson.Create('Mum', 'Blogs', [mary, trev]);  dad := TPerson.Create('Dad', 'Blogs', [mary, trev]);  mary.Mother := mum;  mary.Father := dad;  trev.Mother := mum;  trev.Father := dad;  grandpa := TPerson.Create('Gramps','Blogs', [mum, dad]);  grandma := TPerson.Create('Granny', 'Blogs', [mum, dad]);  mum.Mother := grandma;  mum.Father := grandpa;  grandma.ReportAncestry;  grandpa.ReportAncestry;  mum.ReportAncestry;  dad.ReportAncestry;  mary.ReportAncestry;  trev.ReportAncestry;  mary.Free; trev.Free; mum.Free; dad.Free; grandpa.Free; grandma.Free;end. ---------------------------------------------------------------------- jollytall · 2022-03-09 16:38 Quote from: y.ivanov on March 09, 2022, 01:18:57 pmWhere is the problem? Every person, mother or child has a name. So field Name goes under TPerson. Mother has also her favorite recipe. Child's don't have. So field Recipe should go under TMother. Now, John is a Child. John.Mother.Name is OK, but John.Mother.Recipe is not. It has to be typecasted TMother(John.Mother).Recipe. This is ugly. If they are in the same file, forward declaring TMother solves it, as in my first example. In separate files I could not solve it. Quote from: howardpc on March 09, 2022, 04:24:26 pmAn example: You put everything into one class type. That is not a solution to the problem. It would mean - as per the above example - that TPerson has to have fields like Name (valid for Mother and Child as well), Recipe (not understood for Children) and Toy (not understood for Mothers). Imagine when Mother and Child have hundreds of fields (workplace, husband, salary, car, etc. and nursery, school, etc.) not shared and two fields cross referencing them. In your model, all these fields would be in one class. Beside the point that it would make that class huge (memory, readability concerns) it would also make it impossible to have methods doing different things for the two (what to buy for birthday, alcohol or toy)? ---------------------------------------------------------------------- howardpc · 2022-03-09 17:36 Quote from: jollytall on March 09, 2022, 05:38:07 pmQuote from: howardpc on March 09, 2022, 04:24:26 pmAn example: You put everything into one class type. That is not a solution to the problem. It would mean - as per the above example - that TPerson has to have fields like Name (valid for Mother and Child as well), Recipe (not understood for Children) and Toy (not understood for Mothers). Imagine when Mother and Child have hundreds of fields (workplace, husband, salary, car, etc. and nursery, school, etc.) not shared and two fields cross referencing them. In your model, all these fields would be in one class. Beside the point that it would make that class huge (memory, readability concerns) it would also make it impossible to have methods doing different things for the two (what to buy for birthday, alcohol or toy)? I was proposing my TPerson as a base type, not as a "model" into which you stuff all conceivable fields. As y.ivanov writes, the base class should extract aspects of a person that apply to all people. You clearly require additional declarations for your model and your requirements (requirements you did not state in your first post). So you would want: 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   TAdult = class(TPerson)  public    property FavouriteRecipe: String read FFavouriteRecipe write FFavouriteRecipe;    property Partner: TPerson read FPartner write FPartner;    property Workplace: String read FWorkplace write FWorkplace;    property Salary: Currency read FSalary write FSalary;    etc.  end;   TInfant = class(TPerson)  public    property FavouriteToy: String read FFavouriteToy write FFavouriteToy;    property Nursery: String read FNursery write FNursery;     etc.  end;which is fine. The point is that both infants and adults have names, parents and (potentially) children. ---------------------------------------------------------------------- jollytall · 2022-03-09 18:16 I think you still miss the point. Obviously I can have an ancestor type and two descendants. Also, I can use as a cross reference the ancestor type. The problem is that then I cannot use the AdultInstance.Child.Nursery as a legal structure (AdultInstance is of type TAdult, and it has a Child field, what is type TPerson and TPerson has no Field called Nursery). So if this is the route to follow then the statement is TInfant(AdultInstance.Child).Nursery what is ugly and difficult to read/write when it is used hundred times in the code. If it is all defined in one file, I can safely do (even with TPerson as a common ancestor if they have shared fields and methods): 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 type  TInfant = class; // << This is the key forward declaration that is not possible in different files  TAdult = class(TPerson)    public      Child : TInfant;    public      property FavouriteRecipe: String read FFavouriteRecipe write FFavouriteRecipe;      property Partner: TPerson read FPartner write FPartner;      property Workplace: String read FWorkplace write FWorkplace;      property Salary: Currency read FSalary write FSalary;      etc.    end;  TInfant = class(TPerson)    public      Parent : TAdult;    public      property FavouriteToy: String read FFavouriteToy write FFavouriteToy;      property Nursery: String read FNursery write FNursery;      etc.    end;and then AdultInstance.Child.FavouriteToy and ChildInstance.Parent.Salary work just fine. ---------------------------------------------------------------------- alpine · 2022-03-09 18:30 Quote from: jollytall on March 09, 2022, 05:38:07 pmQuote from: y.ivanov on March 09, 2022, 01:18:57 pmWhere is the problem? Every person, mother or child has a name. So field Name goes under TPerson. Mother has also her favorite recipe. Child's don't have. So field Recipe should go under TMother. Now, John is a Child. John.Mother.Name is OK, but John.Mother.Recipe is not. It has to be typecasted TMother(John.Mother).Recipe. This is ugly. If they are in the same file, forward declaring TMother solves it, as in my first example. In separate files I could not solve it. Quote from: howardpc on March 09, 2022, 04:24:26 pmAn example: You put everything into one class type. That is not a solution to the problem. It would mean - as per the above example - that TPerson has to have fields like Name (valid for Mother and Child as well), Recipe (not understood for Children) and Toy (not understood for Mothers). Imagine when Mother and Child have hundreds of fields (workplace, husband, salary, car, etc. and nursery, school, etc.) not shared and two fields cross referencing them. In your model, all these fields would be in one class. Beside the point that it would make that class huge (memory, readability concerns) it would also make it impossible to have methods doing different things for the two (what to buy for birthday, alcohol or toy)? As for a such a purist approach (typecasting is ugly) I'd recommend to normalize the mother--->child relationship into a separate entity, probably some tuples in a kind of generic container. Or alternatively - to extract and define the interfaces on wich mother and child will interact and later to implement them in a whatever classes. Thus the classes will not cross-reference each other. I doubt any of these will look "less ugly". ---------------------------------------------------------------------- jollytall · 2022-03-09 18:36 I simply stick to the simplest option: keep them in the same file. Works, easy. A bit large, but OK.