Structs & impl
Structs & impl
Defining a Struct
Structs are user-defined composite types with named fields:
struct Vector2: x: i32 y: i32Creating Struct Instances
Call the struct name as a constructor, passing values in field order:
struct Vector2: x: i32 y: i32
let v = Vector2(10, 5)print(v.x) # Output: 10print(v.y) # Output: 5Accessing Fields
Use dot notation to access struct fields:
struct Config: width: i32 height: i32 name: String
let cfg = Config(1920, 1080, "Display")print(cfg.width) # Output: 1920print(cfg.name) # Output: DisplayPassing Structs to Functions
Structs can be passed as function parameters:
struct Vector2: x: i32 y: i32
func dot_product(v: Vector2) -> i32: return v.x * v.y
let v = Vector2(10, 5)let result = dot_product(v)print(result) # Output: 50Implementing Methods with impl
Use impl blocks to attach methods to a struct. Methods receive self as the first parameter:
struct Rect: width: i32 height: i32
impl Rect: func area(self) -> i32: return self.width * self.height
func is_square(self) -> i32: if (self.width == self.height): return 1 else: return 0
func main() -> i32: let r = Rect(10, 20) print(r.area()) # Output: 200
let sq = Rect(5, 5) print(sq.is_square()) # Output: 1 return 0Method Call Syntax
Methods are called using dot notation on the instance:
let r = Rect(10, 20)let a = r.area() # Calls Rect.area(self=r)Operator Overloading
Implement special methods to overload operators:
struct Vec2: x: i32 y: i32
impl Vec2: func __add__(self, other: Vec2) -> Vec2: return Vec2(self.x + other.x, self.y + other.y)
func __mul__(self, other: Vec2) -> i32: return self.x * other.x + self.y * other.y
func main() -> i32: let v1 = Vec2(1, 2) let v2 = Vec2(3, 4)
# Uses __add__ let v3 = v1 + v2 print(v3.x) # Output: 4 print(v3.y) # Output: 6
# Uses __mul__ let dot = v1 * v2 print(dot) # Output: 11 return 0Supported Operator Methods
| Method | Operator | Example |
|---|---|---|
__add__ | + | a + b |
__mul__ | * | a * b |
Structs with String Fields
struct Person: name: String age: i32
let p = Person("Alice", 30)print(p.name) # Output: Aliceprint(p.age) # Output: 30Returning Structs from Functions
Functions can return struct instances:
struct Point: x: i32 y: i32
func origin() -> Point: return Point(0, 0)
func offset(p: Point, dx: i32, dy: i32) -> Point: return Point(p.x + dx, p.y + dy)
func main() -> i32: let o = origin() let moved = offset(o, 5, 10) print(moved.x) # Output: 5 print(moved.y) # Output: 10 return 0Builder Pattern
A common pattern in Thagore (used extensively in the standard library) is to return a modified struct from methods:
struct StringBuilder: total_len: i32 cache: String
func builder_new() -> StringBuilder: return StringBuilder(0, "")
func append(self: StringBuilder, part: String) -> StringBuilder: return StringBuilder(self.total_len + strlen(part), self.cache + part)
func to_string(self: StringBuilder) -> String: return self.cache