Join Nostr
2026-07-09 17:08:48 UTC

binkle on Nostr: I bent Jai into being able to do """member functions""" :devilish: (I bake the index ...

I bent Jai into being able to do """member functions""" :devilish:

(I bake the index to a global array of structs into a runtime function stored on that struct during creation, so when I call that function from the instantiated struct's member it's got a reference to where the data local to it is stored, and I can "using" that so that I can reference the members as though they were local to the function itself). Here's sort of what it looks like:

Gorf :: struct {
TestProc :: ($id: s64) {
using arr[id];
print("(%, %, %)\n", x, y, z);
x += 1.0;
}

f: ();
x, y, z: float;
}

arr: [..] Gorf;

get_id :: () -> s64 {
arr.count += 1; // fake the alloc
return arr.count-1;
}

make_gorf :: (g: Gorf, $id: s64) {
array_add(*arr, g);
arr[id].f = #procedure_of_call arr[id].TestProc(id);
}

Test :: () {
p :: #run get_id();
make_gorf(.{ x = 100.0 }, p);

// print a few times to show x changing
arr[p].f();
arr[p].f();
arr[p].f();
}

#import "Basic";


I'm probably never going to actually use this but it's neat that I could do it if I wanted to