Oct 042019
 

I was just prompted to dig into archive.org to find an ancient (2008) blog post by Alan Bauer A “Nullable” Post which I used back then to implement the nullable types in dzlib.

A comment there mentioned this post on code central (archived). It’s about a StringList value class which frees you of the additional code necessary for creating and freeing a TStringList, so you can e.g. have a function that returns one without leaking memory. Interesting stuff by Janez Makovsek (who back then worked for Dew Research, no idea if he is still there)

StringList is the same as TStringList except that it is a value
type. It does not have to be created, destroyed or put within
try/finally. This is done by the compiler for you. There are
virtually no special performance penalties for these to work:

var
  strings: StringList;
  astr: string;
begin
  strings.Add('test1');
  strings.Add('test2');
  aStr := string(strings);
  RichEdit.Lines.AddStrings(strings);
end;

The code can be used as a template to wrap any TObject as a value class type.

Since Code Central is going to vanish from the face of the earth shortly, I have downloaded that ZIP file so it will be available for posterity.

 Posted by on 2019-10-04 at 16:47