Extension Methods
Extension methods let you attach functions to struct types so they look like methods. GX has no classes or inheritance — this is how you do “methods” without OOP.
Basic Extension Block
Use ex TypeName { ... } to add methods:
struct Player {
name:str
health:i32
score:i32
}
ex Player {
fn is_alive:bool() {
return health > 0
}
fn add_score(points:i32) {
score = score + points
}
}
fn main() {
var p = Player{"Alice", 100, 0}
p.add_score(50)
print("{p.name} score: {p.score}\n")
if (p.is_alive()) {
print("{p.name} is alive\n")
}
}
Inside ex Player the receiver is implicit: you do not declare it as a parameter. The struct’s fields are in scope by their bare names (health, score), and you call the methods with dot syntax: p.is_alive().
self
Every method also has self, a pointer to the receiver. You need it in three situations:
struct Counter {
value:i32
}
fn log_counter(c:*Counter) {
print("counter is {c.value}\n")
}
ex Counter {
fn get:i32() {
return value
}
fn increment() {
value = value + 1
}
// 1. Calling another method of the same type
fn increment_twice() {
self.increment()
self.increment()
}
// 2. A parameter has the same name as a field
fn set(value:i32) {
self.value = value
}
// 3. Handing the whole object to another function
fn log() {
log_counter(self)
}
}
fn main() {
var c = Counter{0}
c.increment()
c.increment_twice()
print("count: {c.get()}\n") // 3
c.set(10)
c.log() // counter is 10
}
- A sibling method is always called through
self:self.increment(). Writing justincrement()is a compile error. - A parameter hides a field of the same name;
self.valuestill reaches the field. selfis writable: any method may modify the receiver. There is no separate read-only form.
Grouping Operations
Methods keep the operations on a type next to each other:
struct Vec3 {
x:f32
y:f32
z:f32
}
ex Vec3 {
fn length_sq:f32() {
return x * x + y * y + z * z
}
fn scale(factor:f32) {
x = x * factor
y = y * factor
z = z * factor
}
fn set(x:f32, y:f32, z:f32) {
self.x = x
self.y = y
self.z = z
}
fn scaled:Vec3(factor:f32) {
return Vec3{x * factor, y * factor, z * factor}
}
}
fn main() {
var v:Vec3
v.set(3.0, 4.0, 0.0)
print("length^2 = {v.length_sq()}\n") // 25
v.scale(2.0)
print("scaled length^2 = {v.length_sq()}\n") // 100
// A method that returns a value can be chained
print("chained = {v.scaled(0.5).length_sq()}\n") // 25
}
Extensions Across Files
You can put the ex block in a different file from the struct definition — even in a different module — as long as both are imported:
// vec.gx
struct Vec3 {
x:f32
y:f32
z:f32
}
// vec_math.gx
ex Vec3 {
fn length_sq:f32() {
return x * x + y * y + z * z
}
}
This lets you add methods to a struct you did not write, for example one that comes from an imported module.
Multiple Extension Blocks
You can have more than one ex block per type, splitting methods by concern:
struct Shape {
x:f32
y:f32
width:f32
height:f32
}
ex Shape {
fn area:f32() {
return width * height
}
fn perimeter:f32() {
return 2.0 * (width + height)
}
}
ex Shape {
fn move_by(dx:f32, dy:f32) {
x = x + dx
y = y + dy
}
fn resize(w:f32, h:f32) {
width = w
height = h
}
}
fn main() {
var s = Shape{0.0, 0.0, 10.0, 5.0}
print("area = {s.area()}\n")
s.move_by(3.0, 4.0)
print("position = ({s.x}, {s.y})\n")
}
Practical Example: Stack Implementation
struct Stack {
items:i32[100]
count:i32
}
ex Stack {
fn push(value:i32) {
if (count < 100) {
items[count] = value
count = count + 1
}
}
fn pop:i32() {
if (count > 0) {
count = count - 1
return items[count]
}
return 0
}
fn is_empty:bool() {
return count == 0
}
fn size:i32() {
return count
}
}
fn main() {
var s:Stack
s.push(10)
s.push(20)
s.push(30)
print("size: {s.size()}\n") // 3
print("pop: {s.pop()}\n") // 30
print("pop: {s.pop()}\n") // 20
print("size: {s.size()}\n") // 1
}
Try it — Create a
Rectstruct and add extension methodscontains(x, y)(checks if a point is inside) andintersects(other)in the Playground.
Expert Corner
What extensions compile to: An extension method fn foo() on type Point becomes a C function named Point_foo(Point* self). Calling p.foo() becomes Point_foo(&p). A bare field name x inside the method becomes self->x. It’s pure syntactic sugar over name-mangled free functions. No vtable, no dispatch overhead, no hidden allocations.
Why ex blocks instead of methods inside structs: Keeping methods separate from struct definitions lets you add methods to types you don’t own, such as structs from imported modules, and split a large type’s methods across files. In Java or C++ you’d need inheritance, mixins, or wrapper classes. GX gives you the same capability with a 2-line ex block.
No inheritance by design: GX has no subclassing, no virtual methods, no abstract base classes. Every method call is a direct function call resolved at compile time. This keeps the language simple and the runtime fast, at the cost of some polymorphism flexibility. For runtime polymorphism, use function pointers in a struct (a C-style vtable) — explicit and under your control.
self is a pointer, not a reference: self inside an ex block is *T. You access fields with self.x, which is auto-deref: it becomes self->x in the generated C. Because it is already a pointer, you pass it as-is to a function that takes *T or out t: *T.
Receivers: All GX extension methods use pointer receivers. Calling a method on a value passes its address; calling it on a pointer (q.foo() with q: *Point) passes the pointer. There’s no “value receiver” option — that would copy the struct on every call. A method can also be called on a temporary, such as the result of another method (v.scaled(0.5).length_sq()).
Method resolution: When you write p.foo(), the compiler searches all ex Point blocks for foo. If none match, it’s “unknown method.” There’s no inheritance walk — just a flat lookup in the extension table for the exact type.