Quick start manual
6-10
Delphi Language Guide
Declaring procedures and functions
Variants can also be used as parameters in overloaded function declarations. Variant
is considered more general than any simple type. Preference is always given to exact
type matches over variant matches. If a variant is passed into such an overload
situation, and an overload that takes a variant exists in that parameter position, it is
considered to be an exact match for the Variant type.
This can cause some minor side effects with float types. Float types are matched by
size. If there is no exact match for the float variable passed to the overload call but a
variant parameter is available, the variant is taken over any smaller float type.
For example:
procedure foo(i: integer); overload;
procedure foo(d: double); overload;
procedure foo(v: variant); overload;
var
v: variant;
begin
foo(1); // integer version
foo(v); // variant version
foo(1.2); // variant version (float literals -> extended precision)
end;
This example calls the variant version of foo, not the double version, because the 1.2
constant is implicitly an extended type and extended is not an exact match for
double. Extended is also not an exact match for variant, but variant is considered a
more general type (whereas double is a smaller type than extended).
foo(Double(1.2));
This typecast does not work. You should use typed consts instead.
const d: double = 1.2;
begin
foo(d);
end;
The above code works correctly, and calls the double version.
const s: single = 1.2;
begin
foo(s);
end;
The above code also calls the double version of foo. Single is a better fit to double
than to variant.
When declaring a set of overloaded routines, the best way to avoid float promotion to
variant is to declare a version of your overloaded function for each float type (Single,
Double, Extended) along with the variant version.
If you use default parameters in overloaded routines, be careful not to introduce
ambiguous parameter signatures. For more information, see “Default parameters
and overloaded routines” on page 6-20.
You can limit the potential effects of overloading by qualifying a routine’s name
when you call it. For example, Unit1.MyProcedure(X, Y) can call only routines declared