Recent

Author Topic: TObjectList with TClass ?  (Read 5380 times)

jojo86

  • Jr. Member
  • **
  • Posts: 52
TObjectList with TClass ?
« on: December 20, 2018, 11:24:42 pm »
Hi,
I would want to use TObjectList with a personal TClass.
For example, imagine that I have a class : TFieldRename
Code: Pascal  [Select][+][-]
  1. Type
  2.   TFieldRename = class
  3.   Name: string;
  4.   Rename: string;
  5.   SQLCaption : String;
  6. end;                    
I want create a TObjectList from this class with many TFieldRename inside.
I tried but it doesn't works :
Code: Pascal  [Select][+][-]
  1. ListRenameFields: TObjectList<TFieldRename>;  
Code: Pascal  [Select][+][-]
  1. ListRenameFields: TList<TFieldRename>;  
Code: Pascal  [Select][+][-]
  1. ListRenameFields: TFPGObjectList<TFieldRename>;  

Maybe I have to add a uses ?
Do you have an example for my problem ?

Thank you very much,
Jojo

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 9589
  • FPC developer.
Re: TObjectList with TClass ?
« Reply #1 on: December 21, 2018, 12:06:18 pm »
For old style generics, you need to be in mode objfpc and include fgl

For Delphi style generics, you need at least FPC 3.2.x, Delphi mode, and need to include unit generics.collections.

Thaddy

  • Hero Member
  • *****
  • Posts: 10926
Re: TObjectList with TClass ?
« Reply #2 on: December 21, 2018, 01:03:51 pm »
Indeed. Example 1:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. uses generics.collections;
  3. Type
  4.   TFieldRename = class
  5.     Name: string;
  6.     Rename: string;
  7.     SQLCaption : String;
  8.   end;                    
  9.  
  10.   TFieldRenameList = specialize TObjectList<TFieldRename>;
  11.  
  12. begin
  13. end.
Example 2:
Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2. uses generics.collections;
  3. Type
  4.   TFieldRename = class
  5.     Name: string;
  6.     Rename: string;
  7.     SQLCaption : String;
  8.   end;                    
  9.  
  10.   TFieldRenameList = TObjectList<TFieldRename>;
  11.  
  12. begin
  13. end.
Example 3:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. uses fgl;
  3. Type
  4.   TFieldRename = class
  5.     Name: string;
  6.     Rename: string;
  7.     SQLCaption : String;
  8.   end;                    
  9.  
  10.   TFieldRenameList = specialize TFPGObjectList<TFieldRename>;
  11.  
  12. begin
  13. end.
Example 4:
Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2. uses fgl;
  3. Type
  4.   TFieldRename = class
  5.     Name: string;
  6.     Rename: string;
  7.     SQLCaption : String;
  8.   end;                    
  9.  
  10.   TFieldRenameList = TFPGObjectList<TFieldRename>;
  11.  
  12. begin
  13. end.

In all cases you can subsequently use:
Code: Pascal  [Select][+][-]
  1. var ListRenameFields:TFieldRenameList;
  2. begin
  3.  ..
  4. end.


« Last Edit: December 21, 2018, 01:10:07 pm by Thaddy »
The average programmer productivity is 4-5 hours per day. Peak performance 72 hours for short bursts. MTBF is 1 second or less.

furious programming

  • Hero Member
  • *****
  • Posts: 516
  • I click a little.
    • TreeStructInfo — format for text and binary configuration files
Re: TObjectList with TClass ?
« Reply #3 on: December 21, 2018, 05:20:29 pm »
In {$MODE DELPHI} we can use the classes from FGL too. Example:

Code: Pascal  [Select][+][-]
  1. {$MODE DELPHI}
  2.  
  3. uses
  4.   FGL;
  5.  
  6. type
  7.   TFieldRename = class
  8.     Name: String;
  9.     Rename: String;
  10.     SQLCaption : String;
  11.   end;
  12.  
  13. type
  14.   TFieldRenameList = TFPGObjectList<TFieldRename>;
  15.  
  16. var
  17.   List: TFieldRenameList;
  18. begin
  19.   List := TFieldRenameList.Create();
  20.   try
  21.     List.Add(TFieldRename.Create());
  22.     List.Add(TFieldRename.Create());
  23.     List.Add(TFieldRename.Create());
  24.   finally
  25.     List.Free();
  26.   end;
  27. end.

And unlike the {$MODE OBJFPC} mode, we don't need to declare an additional list type:

Code: Pascal  [Select][+][-]
  1. {$MODE DELPHI}
  2.  
  3. uses
  4.   FGL;
  5.  
  6. type
  7.   TFieldRename = class
  8.     Name: String;
  9.     Rename: String;
  10.     SQLCaption : String;
  11.   end;
  12.  
  13. var
  14.   List: TFPGObjectList<TFieldRename>;
  15. begin
  16.   List := TFPGObjectList<TFieldRename>.Create();
  17.   try
  18.     List.Add(TFieldRename.Create());
  19.     List.Add(TFieldRename.Create());
  20.     List.Add(TFieldRename.Create());
  21.   finally
  22.     List.Free();
  23.   end;
  24. end.

By the way.
« Last Edit: December 21, 2018, 05:25:04 pm by furious programming »
Lazarus 2.0.12 with FPC 3.2.0 (SVN Revision 64642), Windows 10 — all 64-bit

Thaddy

  • Hero Member
  • *****
  • Posts: 10926
Re: TObjectList with TClass ?
« Reply #4 on: December 21, 2018, 05:28:35 pm »
Why are you duplicating my answer? I covered all.
The average programmer productivity is 4-5 hours per day. Peak performance 72 hours for short bursts. MTBF is 1 second or less.

furious programming

  • Hero Member
  • *****
  • Posts: 516
  • I click a little.
    • TreeStructInfo — format for text and binary configuration files
Re: TObjectList with TClass ?
« Reply #5 on: December 21, 2018, 05:41:22 pm »
Wow, I have overlooked it in a hurry. I'm begging you, forgive me my lord.
Lazarus 2.0.12 with FPC 3.2.0 (SVN Revision 64642), Windows 10 — all 64-bit

440bx

  • Hero Member
  • *****
  • Posts: 2502
Re: TObjectList with TClass ?
« Reply #6 on: December 21, 2018, 06:33:10 pm »
Wow, I have overlooked it in a hurry. I'm begging you, forgive me my lord.
For forgiveness, you have to sacrifice your first gigabyte to a burning generic on mount virtual.
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

furious programming

  • Hero Member
  • *****
  • Posts: 516
  • I click a little.
    • TreeStructInfo — format for text and binary configuration files
Re: TObjectList with TClass ?
« Reply #7 on: December 21, 2018, 09:58:33 pm »
@Thaddy's post does not cover my second example (without using additional type for list). But Ok, my bad.
Lazarus 2.0.12 with FPC 3.2.0 (SVN Revision 64642), Windows 10 — all 64-bit

Thaddy

  • Hero Member
  • *****
  • Posts: 10926
Re: TObjectList with TClass ?
« Reply #8 on: December 23, 2018, 09:35:43 am »
@Thaddy's post does not cover my second example (without using additional type for list). But Ok, my bad.
I explicitly didn't add an example for that, because I don't like such syntax (in either generics mode). The code is much cleaner with the extra type declaration and also better maintainable: it hides the code from cryptic generics declarations on vars.  If you subsequently want to replace a generics derived class with a non-generic one or another generics derived one, the code itself does not change!
The code itself is then completely free of generics handling. These are all in a type block. This is actually how it is supposed to be used. Declaring generics on vars directly is in most cases collateral damage and bad coding style.

In the mean time look at the last part of my example: all 4 examples I gave lead to the same var declaration, so it is transparent.
By using a proper type declaration it does not matter much if the code uses FGL or generics.collections or mode objfpc or mode delphi...
You probably see now why I used a cleaner coding style as compared to using generics straight on the var: such code will become easily convoluted e.g. by a library change or a mode change or even deriving TFieldRenameList from the non-generic TObjectList in the cntnrs unit. All the code stays the same (create, free, add, delete, IndexOf, Sort, etc), except for features that are not common to all those classes mentioned or have a slightly different name.

To me, that is important. It also enables me to write examples on this forum that cover both types of generics syntax with ease. 8-) O:-)
« Last Edit: December 23, 2018, 01:02:13 pm by Thaddy »
The average programmer productivity is 4-5 hours per day. Peak performance 72 hours for short bursts. MTBF is 1 second or less.

furious programming

  • Hero Member
  • *****
  • Posts: 516
  • I click a little.
    • TreeStructInfo — format for text and binary configuration files
Re: TObjectList with TClass ?
« Reply #9 on: December 23, 2018, 08:21:59 pm »
I explicitly didn't add an example for that, because I don't like such syntax (in either generics mode).

I could not know that before—you did not mention it.

Quote
The code is much cleaner with the extra type declaration and also better maintainable: […]

Exactly, I do not deny it. I gave an example without using an additional type because it is one of the possibilities. Possibility, not the necessity. I always declare an additional type myself and at the same time I never use the {$MODE DELPHI}.

I will start using it only when this mode will support inline variable declarations. 8-)
Lazarus 2.0.12 with FPC 3.2.0 (SVN Revision 64642), Windows 10 — all 64-bit

Thaddy

  • Hero Member
  • *****
  • Posts: 10926
Re: TObjectList with TClass ?
« Reply #10 on: December 23, 2018, 09:41:38 pm »
Which will never happen in FPC.
I am merely protecting people from bad habits. I have seen (and written) way too much bad code in my life.  :-X
Point is: If you write working code on the forum some people may start to use it, even if it is bad code.
« Last Edit: December 23, 2018, 09:45:41 pm by Thaddy »
The average programmer productivity is 4-5 hours per day. Peak performance 72 hours for short bursts. MTBF is 1 second or less.

furious programming

  • Hero Member
  • *****
  • Posts: 516
  • I click a little.
    • TreeStructInfo — format for text and binary configuration files
Re: TObjectList with TClass ?
« Reply #11 on: December 23, 2018, 10:06:55 pm »
That's why we need to talk about any possibilities. To know what is a good solution and what is bad, it is needed to get to know all of them, compare them and draw conclusions. Otherwise, a positive or negative opinion has no basis.
Lazarus 2.0.12 with FPC 3.2.0 (SVN Revision 64642), Windows 10 — all 64-bit

PascalDragon

  • Hero Member
  • *****
  • Posts: 3391
  • Compiler Developer
Re: TObjectList with TClass ?
« Reply #12 on: December 25, 2018, 07:07:37 pm »
For old style generics, you need to be in mode objfpc and include fgl

For Delphi style generics, you need at least FPC 3.2.x, Delphi mode, and need to include unit generics.collections.
The generics in Generics.Collections do not require mode Delphi, they can be used perfectly fine with mode ObjFPC.

And unlike the {$MODE OBJFPC} mode, we don't need to declare an additional list type:

Code: Pascal  [Select][+][-]
  1. {$MODE DELPHI}
  2.  
  3. uses
  4.   FGL;
  5.  
  6. type
  7.   TFieldRename = class
  8.     Name: String;
  9.     Rename: String;
  10.     SQLCaption : String;
  11.   end;
  12.  
  13. var
  14.   List: TFPGObjectList<TFieldRename>;
  15. begin
  16.   List := TFPGObjectList<TFieldRename>.Create();
  17.   try
  18.     List.Add(TFieldRename.Create());
  19.     List.Add(TFieldRename.Create());
  20.     List.Add(TFieldRename.Create());
  21.   finally
  22.     List.Free();
  23.   end;
  24. end.
With the addition of generic functions and methods in 3.1.1 the restriction regarding inline types was lifted and thus the code above works in mode ObjFPC for FPC 3.2/3.3.1 as well (once specialize is added to the appropriate locations of course):

Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC}
  2.  
  3. uses
  4.   FGL;
  5.  
  6. type
  7.   TFieldRename = class
  8.     Name: String;
  9.     Rename: String;
  10.     SQLCaption : String;
  11.   end;
  12.  
  13. var
  14.   List: specialize TFPGObjectList<TFieldRename>;
  15. begin
  16.   List := specialize TFPGObjectList<TFieldRename>.Create();
  17.   try
  18.     List.Add(TFieldRename.Create());
  19.     List.Add(TFieldRename.Create());
  20.     List.Add(TFieldRename.Create());
  21.   finally
  22.     List.Free();
  23.   end;
  24. end.

furious programming

  • Hero Member
  • *****
  • Posts: 516
  • I click a little.
    • TreeStructInfo — format for text and binary configuration files
Re: TObjectList with TClass ?
« Reply #13 on: December 26, 2018, 12:56:37 am »
Thank you @PascalDragon for your response. Well, we have another way to use generics. I probably will not use this syntax anyway, but it's good to know.
Lazarus 2.0.12 with FPC 3.2.0 (SVN Revision 64642), Windows 10 — all 64-bit

 

TinyPortal © 2005-2018