#pros of using comptime

1 messages · Page 1 of 1 (latest)

pallid pebble
#

i am new to zig. I was reading about comptime. Can i know what's the advantages of using it? I understand few things about it. I might be wrong. Is it similar to macros?

frigid ingot
#

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.

wide sinew
#

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

gaunt arrow
#

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

wide sinew
#

and it's not quite like macros, who perform "source code level" (or say, text level, token level) manipulations

pallid pebble
#

ok

wide sinew
#

comptime is more like a script you can embed into the compiling process

pallid pebble
#

oh

wide sinew
#

(it's not a perfect analogy but better than macro)

pallid pebble