Idiom # 21: Swap values
See programming-idioms.org:
Code
Since variables in Fuzion are usually immutable, we cannot re-assign new
values, and, in particular, we cannot swap two values. Instead, we can declared
two new variables a¹
and b¹
and assign the swapped
values to these:
Alternatively, we can create two mutable values and assign them to
fields a
and b
. Then, we can mutate these using
the <-
operator.
Running Example
The following code uses mutable values. Note that this renders the code
impure, i.e., analyzing the code for Effects! will show that this
required the mutate
effect.
last changed: 2024-07-01