Skip to main content
Tolk supports structures.

Distinct structure types

Structures with identical fields are not assignable to each other:
Even though SomeA and SomeB have identical layouts, they represent distinct types.

Contextual type inference

The compiler infers types from context. In the example below, the compiler determines that { ... } has type StoredInfo based on the parameter type:
The same applies to return values and assignments:

Explicit type hints

Explicit type hints are available. In addition to the plain { ... } syntax, the form StructName { ... } can be used. The snippet below is equivalent to the previous example:
When neither contextual information nor an explicit type hint is available, the type cannot be inferred and an error is produced.

Methods

Methods are declared as extension functions:
Without self parameter, a method will be static. By default, self is immutable. The form mutate self enables mutation.

Serialization prefixes

Serialization prefixes do not affect typing or layout. The syntax struct (PREFIX) Name { ... } specifies a serialization prefix. The prefix affects only the binary representation; all other aspects remain unchanged.

Structure syntax

Structure syntax includes:
  • shorthand field syntax { x, y };
  • default field values (a: int32 = 10);
  • private and readonly fields;
  • serialization prefixes (opcodes).

Stack layout and serialization

Fields are placed on the stack sequentially and are serialized in the same order. If a struct has a prefix, it is written first. Serialization behavior can be overridden by defining custom serializers.