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
privateandreadonly
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:Empty structures
An empty structure without fields serves as a grouping construct for static methods. For example, standard functionsblockchain.now() and others are declared this way:
self.
Default values for fields
Fields that have defaults may be missed out of a literal:Private and readonly fields
private— accessible only within methodsreadonly— 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() works for “any receiver”.
Method overloading (also known as partial specialization) is also allowed.
Serialization prefixes and opcodes
Syntaxstruct (PREFIX) Name { ... } allows specifying serialization prefixes.
Typically, 32-bit prefixes for messages are called opcodes.
0x000F— 16-bit prefix0b010is 3-bit (binary form, notice ‘0b’)