Skip to main content
Tolk supports structures — similar to TypeScript classes. This page focuses on syntax details only. See also structures in the type system.

Syntax of structures

  • every field has a mandatory type and an optional default value
  • fields are separated by newlines (preferred), commas, or semicolons
  • fields may be private and readonly

Create an object

Use { ... } when the type is clear, or StructName { ... } explicitly.

Shorthand object syntax

Similar to TypeScript, { a, b } means { a: a, b: b }.

Methods for structures

Methods are declared separately as extension functions:
See Functions and methods.

Empty structures

An empty structure without fields serves as a grouping construct for static methods. For example, standard functions blockchain.now() and others are declared this way:
These methods are static because they do not accept self.

Default values for fields

Fields that have defaults may be missed out of a literal:

Private and readonly fields

  • private — accessible only within methods
  • readonly — immutable after object creation
As a consequence, the only way to create an object with a private field is through a static method or an assembler function.

Generic structs

Generics exist only at the type level and incur no runtime cost.
See generics for structures and type aliases.

Methods for generic structs

When parsing the receiver in fun <receiver>.f(), the compiler treats unknown symbols as type parameters:
It’s a generalization. For example, fun T.copy() works for “any receiver”. Method overloading (also known as partial specialization) is also allowed.
See examples in Functions and methods.

Serialization prefixes and opcodes

Syntax struct (PREFIX) Name { ... } allows specifying serialization prefixes. Typically, 32-bit prefixes for messages are called opcodes.
For example, such an outgoing message will start with this hex number:
Prefixes are not restricted to 32 bits:
  • 0x000F — 16-bit prefix
  • 0b010 is 3-bit (binary form, notice ‘0b’)
Non‑32‑bit prefixes are useful for controlling union types when expressing TL‑B’s multiple constructors. Read about serialization of structures.