I took the above idea and implemented it in a tokenreader used for importing data.
Each token is a string, which is then converted to the appropriate type.
The code needed for converting to integer and string is at the bottom of the post.
To read and convert a token only the following code is needed:
Myvalue := DataImporter.ImportToken;
Depending on the type of Myvalue, the correct implicit conversion is triggered.
The code:
TTokenValue = record
public
FValue:string;
constructor Create(const AValue:string);
class operator Implicit(const AFoo:TTokenValue): integer;
class operator Implicit(const AFoo:TTokenValue): string;
end;
Implementation
function TDataImporter.ImportToken: TTokenValue;
begin
result := TTokenValue.Create(GetCurrentToken());
end;
constructor TTokenValue.Create(const AValue: string);
begin
FValue := AValue;
end;
class operator TTokenValue.Implicit(const AFoo: TTokenValue): integer;
begin
result := strtointdef(AFoo.FValue, 0);
end;
class operator TTokenValue.Implicit(const AFoo: TTokenValue): string;
begin
result:=AFoo.FValue;
end;