I am trying to write a function that accepts any TList and returns a string representation of all TList elements.
I tried such a function
function ListToString(list:TList<TObject>):String;
This works fine except that you cannot pass a TList<String> .
E2010 Incompatible types: 'TList<System.TObject>' and 'TList<System.string>'
In Delphi, a string is not an object. To solve this problem, I wrote a second function:
function StringListToString(list:TList<string>):String;
Is this the only solution? Are there other ways to treat String as being more "object-like"?
In a similar vein, I also wanted to write a function called 'equals' to compare two TLists. Again I am facing the same problem
function AreListsEqual(list1:TList<TObject>; list2:TList<TObject>):boolean;
Is it possible to write this function (possibly using generics?) So that it can also handle TList<String> ? Are there any other tricks or “best practices” that I should be aware of when trying to create code that processes both strings and objects? Or am I just creating two versions of each function? Can generics help?
I am from the Java background, but now I work in Delphi. It seems they have been adding a lot of things to Delphi lately from the Java world (or maybe the C # world that copied them from Java). Like adding equals () and hashcode () to a TObject and creating a common collection structure, etc. I wonder how these add-ons are very practical if you cannot use Strings with them.
[edit: Someone mentioned TStringList. I have used this so far, but I am asking about TList. I am trying to work if using TList for everything (including Strings) is a cleaner way to go.]
delphi delphi-2010
awmross
source share