mutate, used both at declaration and invocation, allows to modify an argument.
Value semantics
Function arguments are copied by value. Function calls do not modify the original data.mutate for a parameter
The mutate keyword makes a parameter mutable.
To prevent unintended modifications, mutate must also be specified at the call site.
self in methods is immutable by default
Instance methods are declared as fun <receiver>.f(self).
By default, self is immutable:
mutate self allows modifying the receiver
someSlice.readFlags(), the object is mutated.
Methods for structures are declared in the same way:
How mutate works under the hood
Tolk code is executed by TVM — a stack-based virtual machine.
Mutations work by implicit returning new values via the stack.
Note: T.fromSlice(s) does NOT modify s
Auto-serialization via fromSlice follows absolutely identical rules.
But some developers got stuck on this exact case, let’s highlight it specially.
Given f(anyVariable), the variable remains unchanged.
If a function mutates it, such a call is invalid, a valid is f(mutate anyVariable).
Same goes for AnyStruct.fromSlice(s): a slice is not mutated, its internal pointer is not shifted.
So, calling s.assertEnd() will not actually check “nothing is left after loading AnyStruct”.
fromCell and fromSlice automatically ensure the slice end after reading.
For input 0102FF, an exception 9 is thrown. This behavior can be turned off with an option: