Here's the generally accepted definition of pure functions :
- the function return values are identical for identical arguments (no variation with local static variables, non-local variables, mutable reference arguments or input streams), and
- the function has no side effects (no mutation of local static variables, non-local variables, mutable reference arguments or input/output streams).
Even if the argument here is that structs are value types and not reference types, so there's no variation in mutable reference arguments only mutable value arguments (e.g this doesn't propagate to other bindings because creating an assignment to a value type has copy semantics instead of reference sharing semantics), it still seems to unarguably be a side effect.
an operation, function or expression is said to have a side effect if it modifies some state variable value(s) outside its local environment, which is to say if it has any observable effect other than its primary effect of returning a value to the invoker of the operation
Put another way, the function isn't referentially transparent. Even though prependWorkersHomeStreet()
returns nothing, you can't replace the call to the function with nothing and end up with the same program.
I have tested it in a repl just to double check that I'm not severely misunderstanding what this does:
print(user)
let _: () = user.prependWorkersHomeStreet()
print(user)
the first and third line print different values, and removing the second line (an expression of type ()
), changes the value of the program. Thus, this function emits a side effect and is not pure.