Wu Language
Search…
Structs
The following code shows basic use of a struct.
1
Point: struct {
2
x: float
3
y: float
4
}
Copied!
Comma separation is optional in struct initialization and definition.
Point is thus defined as a data structure containing two float fields xand y.
You can define instances of Pointlike so:
1
point0 := new Point {
2
x: 100
3
y: 200
4
}
5
6
point1: Point = new Point {
7
x: 1
8
y: 2
9
}
Copied!
Struct fields are always mutable. You can access fields of a struct like this:
1
point := new Point {
2
x: 100
3
y: 100
4
}
5
6
point x = 20
7
point y = point x
Copied!
Wu does not use .for field access, because doing so would be gross.
We can also implement methods on our structs. This will feel very familiar for people with Rust experience.
1
Vector: struct {
2
x: float,
3
y: float
4
}
5
6
implement Vector {
7
new: fun(x, y) -> Self {
8
Vector {
9
x: x,
10
y: y
11
}
12
}
13
14
length: fun(self) -> float {
15
(self x^2 + self y^2)^0.5
16
}
17
18
normalize: fun(self) {
19
len := self length()
20
21
self x /= len
22
self y /= len
23
}
24
}
25
26
a := Vector new(100, 100)
27
a normalize()
Copied!
Self aliases whatever struct type you're implementing on.
Last modified 2yr ago
Copy link