Skip to content

Structs

Structs are Nubo’s way to define custom data types.

A struct describes fields and their types. Calling the struct creates an instance.

struct User {
name: string
age: int
}
let user = User()
user.name = "Martin"
user.age = 19
println(user.name)
FeatureDescription
FieldsNamed, typed values stored on each instance.
InstancesCreated by calling the struct, like User().
Type checkingAssigning a field checks the value type.
impl methodsAttach behavior to a struct.
Private fieldsHide fields from outside code.
ConstructorsUse init to initialize instances.
Singleton patternsReturn the same instance from init.
HooksCustomize string conversion, cloning, value conversion, indexing, iteration, and output.
PageDescription
Defining StructsCreate structs, fields, instances, and default values.
FieldsField access, assignment, type checking, and iteration.
Impl MethodsAttach methods to structs and understand automatic self binding.
Constructors and InitInitialize instances with init.
Singleton StructsReturn a shared instance from init.
Private FieldsProtect fields and expose controlled methods.
Struct HooksUse __string__, __clone__, __value__, __get__, __set__, __iterate__, and $convout.
ExamplesComplete struct examples.