Recent

Author Topic: Generics - what am I missing?  (Read 6410 times)

vfclists

  • Hero Member
  • *****
  • Posts: 1165
    • HowTos Considered Harmful?
Generics - what am I missing?
« on: February 25, 2016, 11:52:25 pm »
I looked up some information on using TStringsList.Objects[xxx] on stackoverflow and this is what I learned from the page - http://stackoverflow.com/questions/8947400/tstringlists-addobject-method

It seems that I shouldn't be using TStringList.Objects[xx] in new code, but should be using TList<T>.

Any opinions and code examples from Lazarus developers and users?

Are there examples in the FCL / LCL?
Lazarus 3.0/FPC 3.2.2

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Generics - what am I missing?
« Reply #1 on: February 26, 2016, 12:29:02 am »
It seems that I shouldn't be using TStringList.Objects[xx] in new code, but should be using TList<T>.
I fail to see how TList could replace TStringList if you want to make use of things like Object association with a string or use the other Stringlist functionality that TList not even provides (such as Names, Values, Strings and Text properties).

In case you're using TStringList solely for the purpose for adding some records or objects to a list, then yes it might perhaps be that you are better of using a generics list.

Problem is that the question asked neither the answers given treats this part. The question asked was that asker was confused about the two. The answer given that TStringList is outdated and from ancient times is just plain wrong in that regards -> it depends on what you want to achieve and/or do.

fwiw: in case adding objects to a list then please make life easier for yourself and don't use a normal TList but the special crafted TFPGObjectList. For small example here.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Generics - what am I missing?
« Reply #2 on: February 26, 2016, 12:44:40 am »
It seems that I shouldn't be using TStringList.Objects[xx] in new code, but should be using TList<T>.
erm no, a stringlist with objects can be seen as a map with the key being a string tlist<t> supports a single item and it is equivalent to a TObjectlist in which case there is nothing to be gained from using a specialized TList<T>
Any opinions and code examples from Lazarus developers and users?
If your code already uses tstringlist or tobjectlist there is no need to change it. If there is a hierarchy or interface that you need to use then forget generics it would be a pain to write all that support code for each new list. They are good for fast prototyping but other than that I do not see any reasons to choose one over the other.
Are there examples in the FCL / LCL?

Make sure you add fgl to your uses clause and run the following procedure.

Code: Pascal  [Select][+][-]
  1. procedure Testing;
  2. type
  3.   TObjList = specialize TFPGList<TObject>;
  4. var
  5.   MyList :TObjList;
  6. begin
  7.   MyList := TObjList.Create;
  8.   try
  9.     MyList.Add(Nil);
  10.     mylist.Add(Nil);
  11.     MyList.Add(Nil);
  12.   finally
  13.     MyList.Free;
  14.   end;
  15. end;
  16.  
In short generics might be a good idea for a library writer (not entirely convinced about that yet) but for an application developer where things are less fluid for now I prefer to avoid them, until at least I have a generics library that can support polymorphism as easy as a hierarchy.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Generics - what am I missing?
« Reply #3 on: February 26, 2016, 01:31:27 am »
@taazz:
When concerning objects and generics list, wouldn't it be more comfortable to use TFPGObjectList simply because it has the options to 'own' the added objects (just as TStringList can).?

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Generics - what am I missing?
« Reply #4 on: February 26, 2016, 01:43:34 am »
@taazz:
When concerning objects and generics list, wouldn't it be more comfortable to use TFPGObjectList simply because it has the options to 'own' the added objects (just as TStringList can).?
Yes it would, but where is the example on generics on that? How can the OP compare between the use of a TList<T> and TObjectlist if he can't see any differences or choose one over the other? For me its simple TFPG whatever is not a good name to use and it will only be used if there is no alternative.
« Last Edit: February 26, 2016, 01:47:18 am by taazz »
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Generics - what am I missing?
« Reply #5 on: February 26, 2016, 01:52:24 am »
@taazz:
Ah, yes. You are correct, my bad.

I simply looked at it from the angle that there is no comparison between a TList vs TStringList but, in case of 'abusing' TStringList objects property only for the purpose 'listing' Objects then your comment makes perfectly sense.

Thaddy

  • Hero Member
  • *****
  • Posts: 18787
  • To Europe: simply sell USA bonds: dollar collapses
Re: Generics - what am I missing?
« Reply #6 on: February 26, 2016, 09:48:40 am »
Note that there is also the - almost - Delphi compatible generics.collections available. Not yet in trunk but from https://github.com/dathox/generics.collections
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 1315
Re: Generics - what am I missing?
« Reply #7 on: February 26, 2016, 04:57:49 pm »
I have a question about generics as well: can I make a generic list that only accepts descendants of a certain generic item?

Something like:
Code: [Select]
  generic TItemList<T where T is TListItem<T>> = class
  ..
  end;

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Generics - what am I missing?
« Reply #8 on: February 26, 2016, 08:32:07 pm »
I have a question about generics as well: can I make a generic list that only accepts descendants of a certain generic item?

Something like:
Code: [Select]
  generic TItemList<T where T is TListItem<T>> = class
  ..
  end;
Code: Pascal  [Select][+][-]
  1. generic TItemList<T: TSomeBaseClass> = class
  2.  

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Generics - what am I missing?
« Reply #9 on: February 27, 2016, 05:25:35 am »
Note that there is also the - almost - Delphi compatible generics.collections available. Not yet in trunk but from https://github.com/dathox/generics.collections
Thanks very much for that link Thaddy.

Thaddy

  • Hero Member
  • *****
  • Posts: 18787
  • To Europe: simply sell USA bonds: dollar collapses
Re: Generics - what am I missing?
« Reply #10 on: February 27, 2016, 11:42:55 am »
Thanks very much for that link Thaddy.

Author is a member of this forum.
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 1315
Re: Generics - what am I missing?
« Reply #11 on: February 28, 2016, 06:51:08 pm »
Code: Pascal  [Select][+][-]
  1. generic TItemList<T: TSomeBaseClass> = class
  2.  

Thanks! That was easy. And a nice syntax as well :)

 

TinyPortal © 2005-2018