Fuzion Logo
flang.dev — The Fuzion Language Portal
JavaScript seems to be disabled. Functionality is limited.

Idiom # 21: Swap values

See programming-idioms.org:


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 and 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., analysing the code for Effects! will show that this required the mutate effect.