aboutsummaryrefslogtreecommitdiff
path: root/exercises/067_comptime2.zig
diff options
context:
space:
mode:
authorDave Gauer <dave@ratfactor.com>2021-04-15 20:58:12 -0400
committerDave Gauer <dave@ratfactor.com>2021-04-15 20:58:12 -0400
commit1880b24ea67c595eff4b051cb1f624f2b0ab737d (patch)
treeed4c775edee24d02b8651aa1aceba188eff6baa9 /exercises/067_comptime2.zig
parent6f3ab8b0251fb07d8665ca98dca6510298c76bd2 (diff)
downloadziglings-1880b24ea67c595eff4b051cb1f624f2b0ab737d.tar.gz
ziglings-1880b24ea67c595eff4b051cb1f624f2b0ab737d.tar.bz2
ziglings-1880b24ea67c595eff4b051cb1f624f2b0ab737d.tar.xz
ziglings-1880b24ea67c595eff4b051cb1f624f2b0ab737d.zip
add ex067 comptime TWO
Diffstat (limited to 'exercises/067_comptime2.zig')
-rw-r--r--exercises/067_comptime2.zig69
1 files changed, 69 insertions, 0 deletions
diff --git a/exercises/067_comptime2.zig b/exercises/067_comptime2.zig
new file mode 100644
index 0000000..79f4335
--- /dev/null
+++ b/exercises/067_comptime2.zig
@@ -0,0 +1,69 @@
+//
+// Understanding how Zig treats numeric literals is fundamental
+// and important, but it isn't exactly exciting.
+//
+// We're about to get into the cool wizard stuff that makes
+// programming computers fun. But first, let's introduce a new and
+// vital Zig keyword:
+//
+// comptime
+//
+// When you put 'comptime' in front of a variable declaration,
+// function parameter, or expression, you're saying, "I want Zig
+// to evaluate this at compile time rather than runtime."
+//
+// We've already seen that Zig implicitly performs certain types
+// of evaluations at compile time. (Many compilers do a certain
+// amount of this, but Zig is explicit about it.) Therefore,
+// these two statements are equivalent and using the 'comptime'
+// keyword here is redundant:
+//
+// const foo1 = 5;
+// comptime const foo2 = 5;
+//
+// But here it makes a difference:
+//
+// var bar1 = 5; // ERROR!
+// comptime var bar2 = 5; // OKAY!
+//
+// 'bar1' gives us an error because Zig assumes mutable
+// identifiers will be used at runtime and trying to use a
+// comptime_int of undetermined size at runtime is basically a
+// MEMORY CRIME and you are UNDER ARREST.
+//
+// 'bar2' is okay because we've told Zig that this identifier
+// MUST be resolvable at compile time. Now Zig won't yell at us
+// for assigning a comptime_int to it without a specific runtime
+// size.
+//
+// The comptime property is also INFECTIOUS. Once you declare
+// something to be comptime, Zig will always either:
+//
+// 1. Be able to resolve that thing at compile time.
+// 2. Yell at you.
+//
+const print = @import("std").debug.print;
+
+pub fn main() void {
+ //
+ // In this contrived example, we've decided to allocate some
+ // arrays using a variable count!
+ //
+ // Please make this work. Please?
+ //
+ var count = 0;
+
+ count += 1;
+ var a1: [count]u8 = .{'A'} ** count;
+
+ count += 1;
+ var a2: [count]u8 = .{'B'} ** count;
+
+ count += 1;
+ var a3: [count]u8 = .{'C'} ** count;
+
+ count += 1;
+ var a4: [count]u8 = .{'D'} ** count;
+
+ print("{s} {s} {s} {s}\n", .{a1, a2, a3, a4});
+}