简体   繁体   中英

Does the `overload` keyword make any difference?

Sometimes I have the "overload" keyword after a method that's not overloaded.

Besides readability and maintainability of the code, does this have any other impact that I should be aware of?

The big difference is that when the arguments to a method are incorrect, the error message is significantly better for a non-overloaded method.

program Test;

procedure F(X: Integer);
begin
end;

procedure G(X: Integer); overload;
begin
end;

var
  P: Pointer = nil;

begin
  F(P); // E2010 Incompatible types: 'Integer' and 'Pointer'
  G(P); // E2250 There is no overloaded version of 'G' that can be called with these arguments
end.

More subtly, an overloaded method may be overloaded with functions you have no knowledge of. Consider the standard IfThen function. StrUtils.IfThen exists exactly once:

function IfThen(AValue: Boolean; const ATrue: string;
  AFalse: string = ''): string; overload; inline;

yet it is marked as overload . That's because it's overloaded with Math.IfThen , and if a single unit uses both Math and StrUtils , an unqualified IfThen will resolve to the proper function depending on the arguments, and regardless of the order of the units in the uses list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM