Overloading Procedures and Functions
You can declare more than one routine in the same scope with the same name. This is called overloading.
Overloaded routines must be declared with the overload directive and must have distinguishing parameter lists. For
example, consider the declarations
function Divide(X, Y: Real): Real; overload;
begin
Result := X/Y;
115
end
function Divide(X, Y: Integer): Integer; overload;
begin
Result := X div Y;
end;
These declarations create two functions, both called
Divide
, that take parameters of different types. When you call
Divide
, the compiler determines which function to invoke by looking at the actual parameters passed in the call.
For example,
Divide(6.0, 3.0)
calls the first
Divide
function, because its arguments are real-valued.
You can pass to an overloaded routine parameters that are not identical in type with those in any of the routine's
declarations, but that are assignment-compatible with the parameters in more than one declaration. This happens
most frequently when a routine is overloaded with different integer types or different real types - for example,
procedure Store(X: Longint); overload;
procedure Store(X: Shortint); overload;
In these cases, when it is possible to do so without ambiguity, the compiler invokes the routine whose parameters
are of the type with the smallest range that accommodates the actual parameters in the call. (Remember that real-
valued constant expressions are always of type Extended.)
Overloaded routines must be distinguished by the number of parameters they take or the types of their parameters.
Hence the following pair of declarations causes a compilation error.
function Cap(S: string): string; overload;
...
procedure Cap(var Str: string); overload;
...
But the declarations
function Func(X: Real; Y: Integer): Real; overload;
...
function Func(X: Integer; Y: Real): Real; overload;
...
are legal.
When an overloaded routine is declared in a forward or interface declaration, the defining declaration must repeat
the routine's parameter list.
The compiler can distinguish between overloaded functions that contain AnsiString/PChar and WideString/WideChar
parameters in the same parameter position. String constants or literals passed into such an overload situation are
translated into the native string or character type, which is AnsiString/PChar.
procedure test(const S: String); overload;
procedure test(const W: WideString); overload;
var
a: string;
b: widestring;
begin
a := 'a';
b := 'b';
116
test(a); // calls String version
test(b); // calls WideString version
test('abc'); // calls String version
test(WideString('abc')); // calls widestring version
end;
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.
117
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 in
Unit1;
if no routine in
Unit1
matches the name and
parameter list in the call, an error results.
Dostları ilə paylaş: |