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 = 19Read fields with dot access.
println(user.name)println(user.age)Field Types
Section titled “Field Types”Struct fields have names and types.
struct Project { name: string stars: int tags: []string meta: dict[string, any]}Default Values
Section titled “Default Values”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 = 10counter.label = "views"Struct Type
Section titled “Struct Type”The struct itself is a type definition.
struct User { name: string}Instances have that struct type.
let user = User()
println(type(user))Struct Definitions vs Instances
Section titled “Struct Definitions vs Instances”A struct definition describes the shape.
struct User { name: string}An instance stores values.
let user = User()user.name = "Martin"Empty Structs
Section titled “Empty Structs”Structs can be empty.
struct Marker {}Create an instance:
let marker = Marker()