I can use class operators for strings using an intermediate holding record.
So that I can hide the conversion of built-in types.
program TestNewStringHelper;
{$APPTYPE CONSOLE}
uses
System.SysUtils;
type
TStringRecord = record
private
Data: string;
public
class operator Implicit(const a: TStringRecord): Integer; inline;
class operator Implicit(const a: string): TStringRecord; inline;
end;
{ TStringRecord }
class operator TStringRecord.Implicit(const a: string): TStringRecord;
begin
pointer(Result.Data):= pointer(a);
end;
class operator TStringRecord.Implicit(const a: TStringRecord): Integer;
begin
Result:= StrToInt(a.Data);
end;
var
input: TStringRecord;
output: integer;
begin
input:= '42';
output:= input;
WriteLn(IntToStr(output));
ReadLn;
end.
I would like to do something like:
var
input: string;
output: integer;
begin
input:= '42';
output:= input;
WriteLn(IntToStr(output));
ReadLn;
end.
According to the online help:
Note. Class helpers and records do not support operator overloading.
and http://qc.embarcadero.com/wc/qcmain.aspx?d=72253
Is there a workaround for implicit conversion of built-in types without using an intermediate type?
Johan source
share