← Back to context

Comment by tauoverpi

2 months ago

Comptime is a gateway to partial evaluation, precomputing tables, and data layout optimization using the same language zig that you're familiar with when you need it and just a convenient way to handle generics when you don't.

As an example from my unfinished project, I use it to select the encoding to use [1] when resolving pointers to components [2] to reduce code size [3] as much as possible while keeping a high-level interface [4] where you don't need to care about any of it, just "map" a system over every matching entry within the database and it computes the "best" traversal for a single-thread. This is something that's generally difficult to both keep simple enough to work with and for it to generate good enough code to be worth it while retaining type safety.

Comptime enables such without much cognitive overhead as you're working in the same language as before just with lazy evaluation [5] and slightly more lenient rules around memory management making it easier to focus on working towards the desired the data model.

[1]: https://codeberg.org/tauoverpi/game/src/commit/77ec827ec93bc...

[2]: https://codeberg.org/tauoverpi/game/src/commit/77ec827ec93bc...

[3]: https://codeberg.org/tauoverpi/game/src/commit/77ec827ec93bc...

[4]: https://codeberg.org/tauoverpi/game/src/commit/77ec827ec93bc...

[5]: https://godbolt.org/z/P64Ezcrb7