Discussion on: Zig Interfaces for the Uninitiated, an update

gowind profile image

Great article @kilianvounckx .

Question about the gen struct. We need it, because, as of now , function definitions are not expressions (so you can't write return { .nextFn = fn () {} }) ?
And it looks like the gen struct is created once for every invocation of the Interface's init.
Where is then, the nextImpl fn located. Is it in the stack (as it is returned as a part of the stack) or is it part of the .text section (as it is executable) and in which case gen is a struct with an embedded functionpointer ?

jibal profile image

The gen struct has size zero so it is just an abstraction in the source code ... it doesn't exist in the world anywhere and is never "created".

The nextImpl function is, like all code, located in the text section. Note that, since it is generic, there will be multiple copies of the code, each calling a different ptr_info.Pointer.child.next function.

gen is a struct with an embedded functionpointer ?

No, structs don't contain pointers to their member functions, in Zig or any other language ... there's no need for that. The struct is just a namespace for its methods.

There is a function pointer to nextImpl, but that's the nextFn field of the Iterator struct: .nextFn = gen.nextImpl,