Global comptime counter

Not really: there are some tricks you could try using with closures over comptime var in the current compiler, but these loopholes are planned to be closed to prevent this: make closure over comptime var a compile error; comptime vars become immutable when they go out of scope · Issue #7396 · ziglang/zig · GitHub

There was also a post a little while ago with some ideas on implementing something like this: C/C++ macro challenge #1: BOOST_PP_COUNTER For example, even if you don’t want to rely on comptime var loopholes, you can still (ab)use properties such as the uniqueness of opaque{} types to at least get unique ID values, although they won’t be sequential.

5 Likes