This is related to my previous post about support of stack allocation inside OCaml. I was wondering if it would be possible to compile a toy language to C, and include support for the OCaml garbage collector, as an opt-in for certain variables?
Example code:
struct Point {
x: int;
y: int;
}
function do_something(): int {
local p1: point = {10, 20}; // Stack allocated
let p2: point = {30, 40}; // Heap allocated, traced by the OCaml GC
return p1.x + p2.y; // p1 and p2 can interact
}
Possible? I don’t care so much about the potential slowdown compared to doing a “real” GC.