2021-04-15 20:58:12 -04:00
|
|
|
//
|
2021-04-16 22:15:27 -04:00
|
|
|
// We've seen that Zig implicitly performs some evaluations at
|
|
|
|
// compile time. But sometimes you'll want to explicitly request
|
|
|
|
// compile time evaluation. For that, we have a new keyword:
|
2021-04-15 20:58:12 -04:00
|
|
|
//
|
2021-04-16 22:15:27 -04:00
|
|
|
// . . . o . . * . . .
|
|
|
|
// . * | . . . . . . * . .
|
|
|
|
// --o-- comptime * | .. .
|
|
|
|
// * | * . . . . --*-- . * .
|
|
|
|
// . . . . . . . . . | . . .
|
2021-11-05 11:44:29 -04:00
|
|
|
//
|
2021-04-16 22:15:27 -04:00
|
|
|
// When placed before a variable declaration, 'comptime'
|
|
|
|
// guarantees that every usage of that variable will be performed
|
|
|
|
// at compile time.
|
2021-04-15 20:58:12 -04:00
|
|
|
//
|
2021-04-16 22:15:27 -04:00
|
|
|
// As a simple example, compare these two statements:
|
2021-04-15 20:58:12 -04:00
|
|
|
//
|
|
|
|
// var bar1 = 5; // ERROR!
|
|
|
|
// comptime var bar2 = 5; // OKAY!
|
|
|
|
//
|
2021-04-16 22:15:27 -04:00
|
|
|
// The first one gives us an error because Zig assumes mutable
|
|
|
|
// identifiers (declared with 'var') will be used at runtime and
|
|
|
|
// we have not assigned a runtime type (like u8 or f32). Trying
|
|
|
|
// to use a comptime_int of undetermined size at runtime is
|
|
|
|
// a MEMORY CRIME and you are UNDER ARREST.
|
2021-04-15 20:58:12 -04:00
|
|
|
//
|
2021-06-12 18:15:19 -04:00
|
|
|
// The second one is okay because we've told Zig that 'bar2' is
|
|
|
|
// a compile time variable. Zig will help us ensure this is true
|
2021-04-16 22:15:27 -04:00
|
|
|
// and let us know if we make a mistake.
|
2021-04-15 20:58:12 -04:00
|
|
|
//
|
|
|
|
const print = @import("std").debug.print;
|
|
|
|
|
2021-11-05 12:37:12 -04:00
|
|
|
pub fn main() void {
|
2021-04-15 20:58:12 -04:00
|
|
|
//
|
|
|
|
// In this contrived example, we've decided to allocate some
|
2021-04-16 22:15:27 -04:00
|
|
|
// arrays using a variable count! But something's missing...
|
2021-04-15 20:58:12 -04:00
|
|
|
//
|
2024-06-05 21:01:52 -04:00
|
|
|
comptime var count = 0;
|
2021-04-15 20:58:12 -04:00
|
|
|
|
|
|
|
count += 1;
|
2023-11-21 09:01:22 -05:00
|
|
|
const a1: [count]u8 = .{'A'} ** count;
|
2021-04-15 20:58:12 -04:00
|
|
|
|
|
|
|
count += 1;
|
2023-11-21 09:01:22 -05:00
|
|
|
const a2: [count]u8 = .{'B'} ** count;
|
2021-04-15 20:58:12 -04:00
|
|
|
|
|
|
|
count += 1;
|
2023-11-21 09:01:22 -05:00
|
|
|
const a3: [count]u8 = .{'C'} ** count;
|
2021-04-15 20:58:12 -04:00
|
|
|
|
|
|
|
count += 1;
|
2023-11-21 09:01:22 -05:00
|
|
|
const a4: [count]u8 = .{'D'} ** count;
|
2021-04-15 20:58:12 -04:00
|
|
|
|
2021-11-05 12:37:12 -04:00
|
|
|
print("{s} {s} {s} {s}\n", .{ a1, a2, a3, a4 });
|
2021-04-16 22:15:27 -04:00
|
|
|
|
|
|
|
// Builtin BONUS!
|
|
|
|
//
|
|
|
|
// The @compileLog() builtin is like a print statement that
|
|
|
|
// ONLY operates at compile time. The Zig compiler treats
|
|
|
|
// @compileLog() calls as errors, so you'll want to use them
|
|
|
|
// temporarily to debug compile time logic.
|
|
|
|
//
|
|
|
|
// Try uncommenting this line and playing around with it
|
|
|
|
// (copy it, move it) to see what it does:
|
|
|
|
//@compileLog("Count at compile time: ", count);
|
2021-04-15 20:58:12 -04:00
|
|
|
}
|