Skip to main content
In FunC, methods can be called as .method() or ~method(). In Tolk, all methods use a dot: .method(). A method may or may not mutate the object. Tolk defines a mutability model that generalizes the behavior of the ~ tilde in FunC. Behavior and semantics differ from FunC. Tolk method calls are designed to behave similarly to JavaScript:

Value semantics

By default, function arguments in Tolk are copied by value. Function calls do not modify the original data.
This also applies to slices, cells, and other types:

Mutating function parameters

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

Instance methods and self

Methods — unlike global functions fun f() — are declared as fun receiver_type.f(). If a method accepts self, it is an instance method; otherwise, it is static.
By default, self is immutable. The method cannot modify the object.

Mutating methods with self

Combining mutate with self defines a method that modifies the object and is called using the dot syntax. Example:
The standard library includes many mutate mutate self, such as in tuples and dictionaries. In FunC, equivalent mutating methods use the tilde ~.

Returning self for chaining

Returning self works as return self in Python or return this in JavaScript. It makes methods such as storeInt() chainable.
The return type must be explicitly declared as self. Omitting it causes a compilation error.

Mutate self in asm functions

The same behavior can also be implemented in asm functions. A mutation in the compiler works as an implicit return and reassignment of mutate parameters. Example:
Therefore, an asm function should place self' onto the stack before returning the result:
To return self, specify a return type. The compiler handles the rest:
Low-level constructs are rarely needed. Wrappers around existing functions are usually enough.