Temporary Solution:
By using Delphi's TValue type from the System.Rtti unit, I was able to implement a robust custom Writeln procedure usin overload. Here's how it works:
Main Procedure to Process Arguments
This procedure processes the arguments, determining their types and formatting them as needed:
procedure DoCustomWriteln(const Args: array of TValue);
var
LArg: TValue;
LOutput: string;
I: Integer;
begin
LOutput := '';
for I := Low(Args) to High(Args) do
begin
LArg := Args[I];
case LArg.Kind of
tkInteger: LOutput := LOutput + IntToStr(LArg.AsInteger);
tkFloat: LOutput := LOutput + FloatToStr(LArg.AsExtended);
tkString, tkLString, tkUString, tkWString: LOutput := LOutput + LArg.AsString;
tkChar, tkWChar: LOutput := LOutput + LArg.AsString;
tkVariant:
try
LOutput := LOutput + VarToStr(LArg.AsVariant);
except
LOutput := LOutput + '<invalid variant>';
end;
else
LOutput := LOutput + '<unsupported type>';
end;
// Add a separator unless it's the last argument
if I < High(Args) then
LOutput := LOutput + ', ';
end;
Writeln(LOutput);
end;
Overloading Writeln
To make calling this function straightforward without requiring brackets, I created multiple overloads for the CustomWriteln procedure:
procedure CustomWriteln(A1: TValue); overload;
begin
DoCustomWriteln([A1]);
end;
procedure CustomWriteln(A1, A2: TValue); overload;
begin
DoCustomWriteln([A1, A2]);
end;
procedure CustomWriteln(A1, A2, A3: TValue); overload;
begin
DoCustomWriteln([A1, A2, A3]);
end;
// Add more overloads as needed for additional parameters
Test in Project:
begin
try
// Examples of usage with different types
CustomWriteln(42);
CustomWriteln(3.14, 'Hello');
CustomWriteln(1, 2.2, 'Text', True);
CustomWriteln(1, 'Two', 3.3, 'Four', False, 6);
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
Readln;
end.
Example Output
-------
42
3,14, Hello
1, 2,2, Text, <unsupported type>
1, Two, 3,3, Four, <unsupported type>, 6
Advantages of This Approach:
Flexible Input: Handles integers, floats, strings, characters, and variants.
Type-Safe: Uses TValue to handle types dynamically.
Scalable: Easy to extend by adding more overloads or enhancing DoCustomWriteln.
---
Final Project:
program CustomWritelnProj;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils, System.Variants,
System.Math,
System.Rtti;
procedure DoCustomWriteln(const Args: array of TValue);
var
LArg: TValue;
LOutput: string;
I: Integer;
begin
LOutput := '';
for I := Low(Args) to High(Args) do
begin
LArg := Args[I];
case LArg.Kind of
tkInteger, tkInt64: LOutput := LOutput + LArg.AsInt64.ToString;
tkFloat: LOutput := LOutput + LArg.AsExtended.ToString;
tkEnumeration: LOutput := LOutput + BoolToStr(LArg.AsBoolean, True);
tkString, tkLString, tkUString, tkWString,
tkChar, tkWChar: LOutput := LOutput + LArg.AsString;
tkVariant:
try
LOutput := LOutput + LArg.AsVariant.ToString;
except
LOutput := LOutput + '<invalid variant>';
end;
else
LOutput := LOutput + '<unsupported type>';
end;
// Add a separator unless processing the last element
if I < High(Args) then
LOutput := LOutput + ', ';
end;
Writeln(LOutput);
end;
// Overloaded CustomWriteln implementations
procedure CustomWriteln(A1: TValue); overload;
begin
DoCustomWriteln([A1]);
end;
procedure CustomWriteln(A1, A2: TValue); overload;
begin
DoCustomWriteln([A1, A2]);
end;
procedure CustomWriteln(A1, A2, A3: TValue); overload;
begin
DoCustomWriteln([A1, A2, A3]);
end;
procedure CustomWriteln(A1, A2, A3, A4: TValue); overload;
begin
DoCustomWriteln([A1, A2, A3, A4]);
end;
procedure CustomWriteln(A1, A2, A3, A4, A5: TValue); overload;
begin
DoCustomWriteln([A1, A2, A3, A4, A5]);
end;
procedure CustomWriteln(A1, A2, A3, A4, A5, A6: TValue); overload;
begin
DoCustomWriteln([A1, A2, A3, A4, A5, A6]);
end;
procedure CustomWriteln(A1, A2, A3, A4, A5, A6, A7: TValue); overload;
begin
DoCustomWriteln([A1, A2, A3, A4, A5, A6, A7]);
end;
procedure CustomWriteln(A1, A2, A3, A4, A5, A6, A7, A8: TValue); overload;
begin
DoCustomWriteln([A1, A2, A3, A4, A5, A6, A7, A8]);
end;
procedure CustomWriteln(A1, A2, A3, A4, A5, A6, A7, A8, A9: TValue); overload;
begin
DoCustomWriteln([A1, A2, A3, A4, A5, A6, A7, A8, A9]);
end;
begin
try
// Examples of usage with different types
CustomWriteln(42);
CustomWriteln(MaxComp,'The max value of Int64');
CustomWriteln(MaxComp,MinComp, 'Int64 Interval');
CustomWriteln(1, 2.2, 'Text', True);
CustomWriteln(1, 'Two', 3.3, 'Four', False, 6);
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
Readln;
end.