Skip to main content
Tolk supports structures — similar to TypeScript classes.

Structure syntax

  • Every field has a required type and an optional default value.
  • Fields are separated by newlines, which are preferred, or by commas or semicolons.
  • Fields can be private and readonly.

Object creation

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

Shorthand object syntax

The shorthand { a, b } expands to { a: a, b: b }. This syntax is similar to object shorthand in TypeScript.

Methods for structures

Methods are declared separately as extension functions:

Empty structures

A structure without fields is used as a grouping construct for static methods. For example, standard functions such as blockchain.now() are declared this way:
These methods are static because they do not accept self.

Default values for fields

Fields with default values may be missed out of a literal:

Private and readonly fields

  • private – accessible only within methods.
  • readonly – immutable after object creation.
An object with a private field can only be constructed by a static method or an assembler function.

Generic structs

Generics exist only at the type level and incur no runtime cost.

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() is valid for any receiver. Method overloading or partial specialization is also allowed:

Serialization prefixes and opcodes

The syntax struct (PREFIX) Name { ... } specifies a serialization prefix for the structure. For messages, 32-bit prefixes are typically called opcodes.
An outgoing message starts with the hex number:
Prefixes are not restricted to 32 bits:
  • 0x000F — 16-bit hexadecimal prefix, value 15.
  • 0b010 — 3-bit binary prefix, value 2.
Non-32-bit prefixes are useful for controlling union types when expressing TL-B’s multiple constructors.