Define Zig Enums from Data

This is a small article about something fun I am doing with Zig, spurred on by matklad’s article about partially matching Zig enums.

It uses comptime and zon and the build system to … well … do what it says on the tin.

2 Likes

You can do

const wind_zon = generate_step.captureStdOut();

instead of

const wind_zon = generate_step.addOutputFileArg("data.zon");

to make a more classical UNIX tool which prints to stdout, and lets the decisions as to where the output should go to the user.

But this is Zig. Things are not done with code generation. Things are done with comptime and build systems

comptime has limits, multiple of which are intentional.
For things that require more performance/greatly impact build time, or are too complex to reasonably reason about in comptime/obscure details to an unacceptable degree, code generation is perfectly acceptable and even recommended.

This doesn’t actually work because when there is no extension the compiler assumes it ia a zig file (rather than zon) and gives you the file cannot be a tuple error !

1 Like