#Creating function of previously defined function type

1 messages · Page 1 of 1 (latest)

dense talon
#

Hello,

I'm wondering if zig has a way of defining a function that matches the type of a previously defined function type. In C I would use a macro for this that accepts the name of the function and the macro generates the rest of the signature:

#define UPDATE_AND_RENDER(NAME) void NAME(game_memory *Memory)
typedef UPDATE_AND_RENDER(update_and_render);
UPDATE_AND_RENDER(UpdateAndRenderStub)
{
  (void) Memory;
}

How would I do something like this in Zig?

pulsar quest
#

For all I know, you don't. The closest I can think of, is defining a function with an anytype argument, accepting a tuple, and then you can automatically construct a calling wrapper which checks the types of the tuple's components. It might help, if you explained your use case, maybe there's a different approach.

dense talon
#

The use case is anything that acts like an interface. The signature of the interface functions are known and are the same in all implementations. My only issue is when adding parameters I have to add them everywhere I implement the interface too. Maybe the best and simplest way to do this would be to have a struct containing all parameters of the function. I'm just wondering if there is something in zig that would let me do that in a different way.

pulsar quest
#

Ah, so you want to be able to add new parameters to the signature without having to change the function bodies? This won't work in Zig anyway, since the compiler will complain about unused parameters (unless they are explicitly declared with a _). A single-struct param is probably what you're looking for in this case.

#

Otherwise (if the signature is fixed) it seems you'd be trying to go against Zig spirit of preferring things to be spelled out explicitly.

dense talon
#

Oh, I forgot about that the zig unused parameter thingy. Tbh now that I think about it, the single-struct method seems fine, haha. For some reason it only occured to me while typing here lol

pulsar quest
#

That happens a lot