Dealing with overloaded functions that have ambiguous parameters

delphi, delphi-xe

Solution

Overloaded methods can be very effective. However, as soon as there is a hint of ambiguity they become a liability. A good example of this are the new TStream overloads introduced in XE3. It's not hard to fall into a trap where the compiler chooses an overload that you weren't expecting. At least in your code the compiler stopped. In that sense you were lucky.

So my advice, in your situation, is to abandon overloads. Express the different input types in the method name. Yes it's a little more verbose, but you won't make any mistakes, and you code will compile!

Problem

Take this small example class (not my real code, but it exposes the problem): ``` Convert = class(TObject) public class function ToString(value: Double): String; overload; class function ToString(value: TDateTime): String; overload; end; ``` It compiles fine until you try to use the `Double` or `TDateTime` functions As In: ``` var d: Double; begin d := 99.99; ShowMessage(Convert.ToString(d)); ``` You will get this compile error: Ambiguous overloaded call to 'ToString'. The problem boils down to the fact that `TDateTime` is a type of `Double` My Question: how do You deal with this type of problem? EDIT - I am NOT looking for a solution for the example given I have found 3 Solutions so far: - Rename one of the 2 functions - Add a "Dummy" parameter to one of the 2 functions - Change the parameters to Var types, this has the disadvantage that I can no longer call this function with constants are there any other solutions out there?

Original source