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
privateandreadonly.
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 asblockchain.now() are declared this way:
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.
Generic structs
Generics exist only at the type level and incur no runtime cost.Methods for generic structs
When parsing the receiver infun <receiver>.f(), the compiler treats unknown symbols as type parameters:
fun T.copy() is valid for any receiver.
Method overloading or partial specialization is also allowed:
Serialization prefixes and opcodes
The syntaxstruct (PREFIX) Name { ... } specifies a serialization prefix for the structure. For messages, 32-bit prefixes are typically called opcodes.
0x000F— 16-bit hexadecimal prefix, value 15.0b010— 3-bit binary prefix, value 2.