Exploring comptime
Let's create a sum
function that takes in a slice of numbers (any type of number!!) and returns the sum of all the numbers! How can we do that?
heheh i see you Java/C++ programmers reaching for your < and > hehehe
Woah, what is comptime
? And why is the type of T
like type
itself?
What is comptime?
I'm sure many of us have heard of macros. They exist in languages like C, Rust or even Lisp (in quite a different form), and they serve as a way of executing some code at compile-time instead of runtime.
We might have also heard of generics in Java and Rust, or even templates in C++ (not sure if I'm committing a sin to lump these together), in order to write code that works across all types with certain constraints.
If you're a user of Go, you might've also used go generate
to write repeated code for you.
Well, Zig has a solution that encompasses all three use-cases mentioned above, and that is comptime! What the comptime feature in Zig allows you to do, is simply write Zig code (not any other special language ala C++ template metaprogramming) that is executed at compile-time, instead of runtime!
Pre-computing values
Looking at our earlier example, our sum
function is pure, and can be pre-computed. Let's try to get Zig to precompute the results instead of computing the results at runtime.
Of course, the Zig compiler might have also realised this and done the optimisation already, but for the sake of this being an example of pre-computing values, let's treat this as an optimisation!
Wow! Do you notice what changed? By simply adding the comptime
keyword in front of our call to sum
, the function was called at compile-time instead of runtime.
Generics
Our sum
function is already a pretty good example of using comptime to create the effect of generics. Let's go one step further and create a data structure that supports generics.
Notice how we can return struct
s from functions in Zig! This definitely wouldn't work at runtime, since types don't have a representation at runtime (unless we use runtime reflection). So Zig is actually running the Vector2D
function at comptime here, and treating the resulting structs as types to be constructed.
Remember that this is all Zig code, which means we can go one step further and make the length generic as well!
Here, both parameters to Vector
play a part in defining what kind of struct
the resulting type will be!
Last updated