Skip to content

Structs & impl

Structs & impl

Defining a Struct

Structs are user-defined composite types with named fields:

struct.tg
struct Vector2:
x: i32
y: i32

Creating Struct Instances

Call the struct name as a constructor, passing values in field order:

create_struct.tg
struct Vector2:
x: i32
y: i32
let v = Vector2(10, 5)
print(v.x) # Output: 10
print(v.y) # Output: 5

Accessing Fields

Use dot notation to access struct fields:

field_access.tg
struct Config:
width: i32
height: i32
name: String
let cfg = Config(1920, 1080, "Display")
print(cfg.width) # Output: 1920
print(cfg.name) # Output: Display

Passing Structs to Functions

Structs can be passed as function parameters:

struct_param.tg
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: 50

Implementing Methods with impl

Use impl blocks to attach methods to a struct. Methods receive self as the first parameter:

methods.tg
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 0

Method Call Syntax

Methods are called using dot notation on the instance:

method_call.tg
let r = Rect(10, 20)
let a = r.area() # Calls Rect.area(self=r)

Operator Overloading

Implement special methods to overload operators:

operators.tg
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 0

Supported Operator Methods

MethodOperatorExample
__add__+a + b
__mul__*a * b

Structs with String Fields

string_struct.tg
struct Person:
name: String
age: i32
let p = Person("Alice", 30)
print(p.name) # Output: Alice
print(p.age) # Output: 30

Returning Structs from Functions

Functions can return struct instances:

return_struct.tg
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 0

Builder Pattern

A common pattern in Thagore (used extensively in the standard library) is to return a modified struct from methods:

builder.tg
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