Skip to main content
Tolk follows value semantics: when calling a function, arguments are copied by value. There are no “pointers” or “references to objects”. Nevertheless, the keyword 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.
This also applies to slices, cells, and other types:

mutate for a parameter

The mutate keyword makes a parameter mutable. To prevent unintended modifications, mutate must also be specified at the call site.
This also applies to slices and other types:
A function can define multiple mutate parameters:

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

Thus, when calling someSlice.readFlags(), the object is mutated. Methods for structures are declared in the same way:
A mutating method may even modify another variable:

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.
Mutating methods work literally the same:
For detailed examples of stack ordering, follow assembler functions.

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”.
To check that a slice does not contain excess data, no special actions required, because 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:
For more details and examples, proceed to automatic serialization.