Two identical structures are not assignable
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:
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:
Methods for structures
Methods are declared as extension functions, similar to Kotlin: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
Syntaxstruct (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 privateandreadonlyfields- Serialization prefixes (opcodes)