Generic swap: Difference between revisions

Content added Content deleted
Line 271: Line 271:


=={{header|Delphi}}==
=={{header|Delphi}}==
{{incorrect|Delphi|Option 1 demonstrates a dubious arithmetic trick for swapping integers that works on two's complement hardware thanks to wrapping behavior of arithmetic overflow. Neither option demonstrates generic programming: swapping two variables using the same function or operator, regardless of their type.}}

Option 1:
<lang Delphi>
<lang Delphi>
procedure Swap(var a, b: Integer);
procedure Swap<T>(var a, b: T);
begin
a := a + b;
b := a - b;
a := a - b;
end;
</lang>
Option 2:
<lang Delphi>
procedure Swap(var a, b: Integer);
var
var
temp: Integer;
temp: T;
begin
begin
temp := a;
temp := a;