Recent

Author Topic: TFPGList vs TFPGObjectList in FGL  (Read 3495 times)

simone

  • Hero Member
  • *****
  • Posts: 573
TFPGList vs TFPGObjectList in FGL
« on: August 14, 2019, 03:31:36 pm »
What is the difference between TFPGList and TFPGObjectList in FGL? I know that with TFPGObjectList it is possible to set the ownership of objects, allowing automatic freeing. Are there any other significant differences? Thanks.
Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: TFPGList vs TFPGObjectList in FGL
« Reply #1 on: August 15, 2019, 11:19:37 am »
That's indeed the difference between them (just like TList and TObjectList). In 3.2 and newer TFPGObjectList will even explicitely reject specializations with anything that's not a descendant of TObject.

simone

  • Hero Member
  • *****
  • Posts: 573
Re: TFPGList vs TFPGObjectList in FGL
« Reply #2 on: August 15, 2019, 12:31:04 pm »
Thanks PascalDragon. I'm not sure to understand. In TList (belonging to RTL), items are pointers, whereas in TObjectList (in FCL) they are objects. Moreover in the TList I can't set not ownership of items, conversely in TObjectList I can. Now in FGL implementation of generics, both TFPGList and TFPGObjectList require that items are objects (i.e. descendants of TObject). As a conseguence, metatype T can't be specialized with a record. Differently, in rtl-generics implementation, specialization with a record type is supported. So, what is the impact of change you are talking about? Thanks again for kind support.
Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: TFPGList vs TFPGObjectList in FGL
« Reply #3 on: August 15, 2019, 12:36:53 pm »
TFPGObject list previously silently accepted things that were not class instances (objects is the wrong term. Now that is prevented.
Specialize a type, not a var.

Zoran

  • Hero Member
  • *****
  • Posts: 1829
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: TFPGList vs TFPGObjectList in FGL
« Reply #4 on: August 15, 2019, 01:35:49 pm »
objects is the wrong term.

The term object was absolutely correctly used there.

In common OOP terminology, object is class instance.
Unfortunately, as Borland's legacy, FPC has another usage of the same term.

I'm not saying that old-style objects don't have their place still. I think that not following Delphi in deprecating them is a FPC team's good decision.
However, in modern FPC usage, as well as in Delphi, the old style objects are not common. In Delphi they are even deprecated, so the main usage of the term is just -- class instance.

When talking about objects in different meaning, only then I would add some attribute -- old style objects, old objects, legacy objects, TP-style objects, ...

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: TFPGList vs TFPGObjectList in FGL
« Reply #5 on: August 16, 2019, 09:55:01 am »
Thanks PascalDragon. I'm not sure to understand. In TList (belonging to RTL), items are pointers, whereas in TObjectList (in FCL) they are objects. Moreover in the TList I can't set not ownership of items, conversely in TObjectList I can. Now in FGL implementation of generics, both TFPGList and TFPGObjectList require that items are objects (i.e. descendants of TObject). As a conseguence, metatype T can't be specialized with a record. Differently, in rtl-generics implementation, specialization with a record type is supported. So, what is the impact of change you are talking about? Thanks again for kind support.
No, TFPGList does not require your items to be descendants of TObject. The only requirement is that a = operator is available for the type (Note: global operators don't work, for records it needs to be part of the type).
And the impact is only that TFPGObjectList explicitely checks for descendants of TObject, before that it was implicitely.

simone

  • Hero Member
  • *****
  • Posts: 573
Re: TFPGList vs TFPGObjectList in FGL
« Reply #6 on: August 17, 2019, 10:40:11 am »
Just to improve my knowledge, I have tried the following code:

Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$mode objfpc}{$H+}
  3.  
  4. uses
  5.   Classes,FGL;
  6.  
  7. type
  8.   TItem=record
  9.     Name : string;
  10.     Age : byte;
  11.   end;
  12.  
  13.   TMyList=specialize TFPGList<TItem>;
  14.  
  15.   operator = (X, Y : TItem) R : boolean;
  16.   begin
  17.     R:=(X.Name=Y.Name) and (X.Age=Y.Age);
  18.   end;
  19.  
  20. begin
  21. end

Again, compiler give me the following message:

Error: Operator is not overloaded: "TItem" = "TItem"

I read some old bug report about this topic, but a this point I don't understand if issues about record specialization in FGL has been solved or I'm doing something wrong. Thanks.   
Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: TFPGList vs TFPGObjectList in FGL
« Reply #7 on: August 17, 2019, 11:00:49 am »
Error: Operator is not overloaded: "TItem" = "TItem"
Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC}{$H+}
  2. {$MODESWITCH ADVANCEDRECORDS}
  3.  
  4. uses fgl;
  5.  
  6. type
  7.   TItem = record
  8.     Name: string;
  9.     Age: byte;
  10.     class operator =(const X, Y: TItem): Boolean;
  11.   end;
  12.  
  13.   TMyList = specialize TFPGList<TItem>;
  14.  
  15. class operator TItem.=(const X, Y: TItem): Boolean;
  16. begin
  17.   Result := (X.Age = Y.Age) and (X.Name = Y.Name);
  18. end;
  19.  
  20. begin
  21. end.

simone

  • Hero Member
  • *****
  • Posts: 573
Re: TFPGList vs TFPGObjectList in FGL
« Reply #8 on: August 17, 2019, 11:06:33 am »
Thanks ASerge. I tried your solution, but I forgot {$MODESWITCH ADVANCEDRECORDS}...
Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

simone

  • Hero Member
  • *****
  • Posts: 573
Re: TFPGList vs TFPGObjectList in FGL
« Reply #9 on: August 17, 2019, 11:38:07 am »
Since in my code operator oveloading of = has been correctly done for conventional record, does it means that in FGL specialization is only possible using advanced record with has been correctly done overload, but is not available for conventional record?
« Last Edit: August 17, 2019, 03:04:00 pm by simone »
Microsoft Windows 10 64 bit - Lazarus 3.0 FPC 3.2.2 x86_64-win64-win32/win64

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: TFPGList vs TFPGObjectList in FGL
« Reply #10 on: August 17, 2019, 01:55:11 pm »
does it means that in FGL specialization is only possible using advanced record with equal overload, but is not available for conventional record?
Yes. See https://bugs.freepascal.org/view.php?id=15480

 

TinyPortal © 2005-2018