Where each MtxVec type may be declared:
| Declaration context | Value types (Vector, Matrix, VectorInt, MatrixInt) | Class types (TVec, TMtx, TVecInt, TMtxInt) |
|---|---|---|
| Local variable | YES (primary use) | YES — with CreateIt/FreeIt |
| Class/record field | NO — never | YES — with constructor/destructor |
Rule 1: Value types must only be local variables. Never use them as fields.
Rule 2: Class types as fields — use constructor + destructor (not CreateIt/FreeIt):
TSomething = class
FData: TVec;
constructor Create;
destructor Destroy; override;
end;
constructor TSomething.Create;
begin FData := TVec.Create; end;
destructor TSomething.Destroy;
begin FData.Free; inherited; end;
Rule 3: CreateIt / FreeIt are for local objects only — never for fields.
StringList — value type for string lists (unit: StringVar)
StringList is a value-type record wrapper around TStringList. It has the same API as TStringList (Add, Delete, LoadFromFile, SaveToFile, Count, Strings[], Text, Sort, Find, IndexOf, etc.) but requires no Create or Free — the compiler manages its lifetime automatically via an internal interface reference.
Use StringList instead of TStringList to avoid Create/Free boilerplate. It can be used as a local variable or as a field in classes or records. Unlike Vector/Matrix, StringList has no memory pool caching — each instance manages its own internal TStringList via interface reference counting.
// No Create, no Free, no try/finally
var sl: StringList;
begin
sl.Add('line 1');
sl.Add('line 2');
sl.SaveToFile('output.txt');
end; // freed automatically
StringList auto-converts to TStrings and TStringList via implicit operators, so it can be passed directly to any method expecting those types.
See also: topic type-selection for choosing between value types and class types.