Utility functions based on Object Pascal's runtime type information (RTTI)
Include 'TypeUtils.pas' file in your project folder and add it to 'uses' clause (see examples bellow).
To run the tests just compile and run 'TypeUtilsTests.pas'.
function ToStr(const AValue; ATypeInfo: PTypeInfo): AnsiString;
Automatically converts any value to a string representation, including records and arrays, in the same spirit of 'print' functions found in scripting languages. Its usefull for logging and debugging.
- AValue: The variable to convert to string.
- ATypeInfo: The type information returned by 'TypeInfo' compiler intrinsic.
The string representation of the first parameter.
Print an array of integers:
program Test;
uses TypeUtils;
var
Arr: array[1 .. 3] of Integer = (1, 2, 3);
begin
WriteLn(ToStr(Arr, TypeInfo(Arr))); //Outputs: [1, 2, 3]
end.
Print an array of records:
program Test;
uses TypeUtils, SysUtils;
type
TEmployee = record
FName: ShortString;
FId: LongInt;
FSalary: Currency;
end;
var
Employees: array of TEmployee;
I: LongInt;
begin
SetLength(Employees, 2);
for I := 0 to High(Employees) do
begin
with Employees[I] do
begin
FName := 'John Doe ' + I.ToString;
FId := I;
FSalary := 2000.25 * I;
end;
end;
WriteLn(ToStr(Employees, TypeInfo(Employees))); // Outputs: [('John Doe 0', 0, 0), ('John Doe 1', 1, 2000.25)]
end.