#pros of using comptime
1 messages · Page 1 of 1 (latest)
Comptime is how generics and conditional compilation is done in zig. It's less the benefits and more "its the only way to do it".
If you mean benefits compared to other languages, compared to C, using comptime boolean conditions for conditional compilation is way simpler than macros. For generics, comptime has the benefit over most languages that a type parameter is not part of a different meta-language like cpp templates.
You can also use comptime to do reflection and create some neat abstractions.
Check out cztomsik's tokamak web framework if you want an example of an abstraction achieved thanks to comptime. Basically, tokamak imitates express's middleware chaining using only comptime: https://github.com/cztomsik/tokamak
comptime is not 'better' than regular code. it does a different kind of job (or jobs)
in Zig, "jobs" include reflection, generic, etc. they are not actual business logic, but can provide brilliant support for it
you guys are forgetting the the simplest, least fanciest but most important benefit of them all:
With comptime you are guaranteed that code is run once, only once, the resulting data is embedded into the binary and from then on requires no allocations an is completely immutable
hmm interesting
ohk i got it little bit
hmm i got it
and it's not quite like macros, who perform "source code level" (or say, text level, token level) manipulations
ok
comptime is more like a script you can embed into the compiling process
oh
(it's not a perfect analogy but better than macro)
one of the neatest code i have ever seen. Tbh, this man wrote great code