Function overloading by return type?
has a very detailed answer on the rational on function overloading by return type, and from what I can see Delphi does not allow this, but are there any workarounds to overload a function based on different return type in Delphi?
You can take the "result" as a parameter.
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:
Depending on the type of Myvalue, the correct implicit conversion is triggered.
The code:
Implementation
The implicit and explicit conversion operators for records permit overloading by return type: namely, the type being converted to:
Depending on the context, using a value of type
TFoo
will call the appropriate implicit conversion. If trying to use a value of typeTFoo
as an argument to an overloaded routine that can take either Integer or string in that position, an overload error will occur: