I'm trying to store functions in an arraylist (or an array that just gets extended, but every time I try I just get an error along the lines of error: function pointers must be single pointers, my code currently is pub fn addSchedule() type { const allocator = std.heap.GeneralPurposeAllocator(.{}){}; return std.ArrayList(fn () void).init(allocator.allocator()); }
and so, I ask again, is it possible
#is an arrayList/array of functions possible
1 messages · Page 1 of 1 (latest)
you need to use *const fn () void as the list item type
fn () void is a function, not a function pointer, and that type has to be comptime known
alright thank you now I get to deal with allocation based errors instead
an allocated value must never outlive the allocator it was sourced from. in Zig it is customary for allocating functions to request an argument of type std.mem.Allocator, the function will use said argument for its allocations. in simple programs it is often sufficient to have only one general-purpose allocator, that is initialised at the beginning of main, and passed to the rest of the program:
var gpa: std.heap.GeneralPurposeAllocator(.{}) = .{};
defer std.debug.assert(gpa.deinit() == .ok); // assert no memory leaks
const allocator = gpa.allocator(); // pass `allocator` to functions that need it
```with this, `addSchedule` will have an added input argument of type `std.mem.Allocator`
also what's going on with addSchedule's return type?
also, addSchedule’s return type is type, but it should be std.ArrayList(*const fn() void)
i was experimenting a bunch and forgot to change it
with this,
addSchedulewill have an added input argument of typestd.mem.Allocator
is there any way to make it so addSchedule doesn't have the input argument, the purpose of this function is to handle all that automatically instead of having to manually allocate it every time I want a new schedule
if the function allocates, it should have an allocator argument.
to be fair I'm not entirely sure what you're trying to do, so I can't comment on whether you want the function to be allocating in the first place
I'm trying to create a function that does the creation of an arraylist for the function schedules in my game engine
pub const Start = world.addSchedule();
pub const PreUpdate = world.addSchedule();
pub const Update = world.addSchedule();
pub const PostUpdate = world.addSchedule();
pub const FixedUpdate = world.addSchedule();
pub const InputUpdate = world.addSchedule();```
yeah ughh... I'm not sure if this API design will work
can you write it in another language, and we'll try translating?