Skip to content

Defining Structs

Use struct to define a custom data type.

struct User {
name: string
age: int
}

Create an instance by calling the struct.

let user = User()

Assign fields after creation.

user.name = "Martin"
user.age = 19

Read fields with dot access.

println(user.name)
println(user.age)

Struct fields have names and types.

struct Project {
name: string
stars: int
tags: []string
meta: dict[string, any]
}

When a new instance is created, Nubo initializes all declared fields with default values for their types.

struct Counter {
value: int
label: string
}
let counter = Counter()
println(counter.value)
println(counter.label)

You can then assign actual values.

counter.value = 10
counter.label = "views"

The struct itself is a type definition.

struct User {
name: string
}

Instances have that struct type.

let user = User()
println(type(user))

A struct definition describes the shape.

struct User {
name: string
}

An instance stores values.

let user = User()
user.name = "Martin"

Structs can be empty.

struct Marker {}

Create an instance:

let marker = Marker()