Great job.

Lately, I’ve been quite busy working on my game, specifically on world rendering. In many places, I need to swap the values of two variables, whether to swap vertices or sort elements to determine the final Z-order. Unfortunately, Free Pascal has neither an operator for swapping variable values nor even intrinsics for this purpose. There are
Swap functions, but they swap bytes/words within a given variable, not between two variables.
What do you think about adding a new operator for this purpose? Inspired by Pascal syntax and similar operators from other programming languages (mostly
Icon, such an operator could be
:=:, and could be used in the following way:
First :=: Second; // Swap values between "First" and "Second" variable.
This would be equivalent to this:
var
Temp: Integer;
{..}
Temp := First;
First := Second;
Second := Temp
The reason is that currently neither the language nor the RTL supports a simple way to swap the values of two variables, which forces developers to allocate an additional variable, correctly write the code to swap the data between pairs of variables, and in some cases even wrap the swap in an additional grouping block (if it is the only operation, e.g., after a condition is met). Introducing a new operator would reduce the number of lines of code to one from even seven and minimize the risk of errors.
This operator would have to be used only in conjunction with two variables (just like the assignment operator) of the same data types, or similar types when dealing with some specific data types (e.g., swapping the values of variables of type
Byte and
Word should be valid). In addition to ensuring data type compatibility, support for range testing must also be provided. It is important here to support not only the swapping of simple data types, but also, for example, the swapping of entire structures, arrays, etc., as well as support for generics.
Let me know what you think. Personally, I believe that such an operator would be very useful, would have a wide range of applications, and would also reduce the amount of code that needs to be written, thereby minimizing the chance of errors (or even eliminating the possibility of making a mistake).
PS: Please don't use arguments like
"no, because this operator is too similar to the assignment operator and it's easy to make a mistake here", because that's not a valid argument. I might just as well say that the
:= operator is too similar to the
= operator, that
>= is too similar to
<=, or that
+= is too similar to
*= (or
shl is too similar to
shr).