#Idiomatic way to setup "self" pointers in .init() functions ?

1 messages · Page 1 of 1 (latest)

summer schooner
#

Hello, I'm writing code around a C library.

I'm trying to make a Window struct that contains data linked to the lifetime of a zglfw.Window, the init() should create the zglfw.Window and the deinit() should close it. That means that if I have a non null Window struct, I'm guaranteed to have a proper zglfw.Window open.

Now here the tricky part, in zig we like to have the init() function return a value, not a pointer (see this discussion here https://discord.com/channels/605571803288698900/1192506137900241047). But that means that in my init function I cannot take a pointer to my Window struct to give it to the zglfw api to for example set a user pointer.

The best solution I found is to have a second set_window_pointer() function that must be called after the init() that can do the proper pointer setups, but is there a better way to do this than to have two separate functions ?

Example code for my application :
https://zigbin.io/b4cdad

Thanks !

thick stone
#

you can init your struct to undefined and then call its init function

#

or you can have init allocate, but that's kinda pointless imo