Skip to main content
Tolk supports structures — similar to TypeScript classes, but designed to operate on TVM.

Two identical structures are not assignable

Even though A and B have identical body, they represent distinct types. Analogy: a struct behaves like a TypeScript class, not interface. Typing is nominal, not structural.

The compiler infers the type from context

In a snippet below, the compiler understands that { ... } is StoredInfo because of parameter’s type:
The same applies to return values and assignments:

Explicit type hints are also available

Besides the plain { ... } syntax, the form StructName { ... } may be used, similar to Rust. The snippet below is equivalent to the above:
When neither contextual information nor an explicit type hint is available, the type cannot be inferred and an error is produced.

Methods for structures

Methods are declared as extension functions, similar to Kotlin:
Notice the first self parameter. Without it, a method will be static. By default, self is immutable. The form mutate self enables mutation. Read Functions and methods.

Prefixes do not affect typing or layout

Syntax struct (PREFIX) Name { ... } specifies a serialization prefix. It affects binary representation only, nothing else changes:

Syntax of structures

  • Shorthand syntax { x, y } is available
  • Default values for fields: a: int32 = 10
  • private and readonly fields
  • Serialization prefixes (opcodes)
Read Syntax of structures and fields.

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. For details, follow TVM representation and Serialization.