.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:
| FunC | Tolk |
|---|---|
int flags = cs~load_uint(32); | var flags = cs.loadUint(32); |
(cs, int flags) = cs.load_uint(32); | var flags = cs.loadUint(32); |
(slice cs2, int flags) = cs.load_uint(32); | var cs2 = cs; var flags = cs2.loadUint(32); |
slice data = get_data().begin_parse(); int flag = data~load_uint(32); | val flag = contract.getData().beginParse().loadUint(32); |
dict~udict_set(...); | dict.set(...); |
b~store_uint(x, 32); | b.storeInt(x, 32); |
b = b.store_int(x, 32); | b.storeInt(x, 32); // also works b = b.storeUint(32); |
b = b.store_int(x, 32).store_int(y, 32); | b.storeInt(x, 32).storeInt(y, 32); // also works b = ...; |
Value semantics
By default, function arguments in Tolk are copied by value. Function calls do not modify the original data.Mutating function parameters
Adding themutate keyword makes a parameter mutable. To prevent unintended modifications, mutate must also be specified when calling the function.
Instance methods and self
Methods — unlike global functionsfun f() — are declared as fun receiver_type.f().
If a method accepts self, it is an instance method; otherwise, it is static.
self is immutable. The method cannot modify the object.
Mutating methods with self
Combiningmutate with self defines a method that modifies the object and is called using the dot syntax.
Example:
~.
Returning self for chaining
Returningself works as return self in Python or return this in JavaScript. It makes methods such as storeInt() chainable.
self. Omitting it causes a compilation error.
Mutate self in asm functions
The same behavior can also be implemented inasm functions.
A mutation in the compiler works as an implicit return and reassignment of mutate parameters.
Example:
asm function should place self' onto the stack before returning the result: