aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDave Gauer <dave@ratfactor.com>2021-04-30 20:25:59 -0400
committerDave Gauer <dave@ratfactor.com>2021-04-30 20:25:59 -0400
commit060e2b9789fe1dab38aacdd47e9423d0d1a3bb2c (patch)
tree2e13f7483761531095f83777e3cbb5ff5684be86
parentf3cf67cd65816a81bea5c44f59761a8a3945b7eb (diff)
add ex074 comptime 9
-rw-r--r--exercises/074_comptime9.zig62
-rw-r--r--patches/patches/074_comptime9.patch4
2 files changed, 66 insertions, 0 deletions
diff --git a/exercises/074_comptime9.zig b/exercises/074_comptime9.zig
new file mode 100644
index 0000000..b644536
--- /dev/null
+++ b/exercises/074_comptime9.zig
@@ -0,0 +1,62 @@
+//
+// In addition to knowing when to use the 'comptime' keyword,
+// it's also good to know when you DON'T need it.
+//
+// The following contexts are already IMPLICITLY evaluated at
+// compile time, and adding the 'comptime' keyword would be
+// superfluous, redundant, and smelly:
+//
+// * The global scope (outside of any function in a source file)
+// * Type declarations of:
+// * Variables
+// * Functions (types of parameters and return values)
+// * Structs
+// * Unions
+// * Enums
+// * The test expressions in inline for and while loops
+// * An expression passed to the @cImport() builtin
+//
+// Work with Zig for a while, and you'll start to develop and
+// intuition for these contexts. Let's work on that/ now.
+//
+// You have been given just one 'comptime' statement to use in
+// the program below. Here it is:
+//
+// comptime
+//
+// Just one is all it takes. Use it wisely!
+//
+const print = @import("std").debug.print;
+
+// Being in the global scope, everything about this value is
+// implicitly required to be known compile time.
+const llama_count = 5;
+
+// Again, this value's type and size must be known at compile
+// time, but we're letting the compiler infer both from the
+// return type of a function.
+const llamas = makeLlamas(llama_count);
+
+// And here's the function. Note that the return value type
+// depends on one of the input arguments!
+fn makeLlamas(count: usize) [count]u8 {
+ var temp: [count]u8 = undefined;
+ var i = 0;
+
+ // Note that this does NOT need to be an inline 'while'.
+ while (i < count) : (i += 1) {
+ temp[i] = i;
+ }
+
+ return temp;
+}
+
+pub fn main() void {
+ print("My llama value is {}.\n", .{llamas[2]});
+}
+//
+// The lesson here is to not pepper your program with 'comptime'
+// keywords unless you need them. Between the implicit compile
+// time contexts and Zig's aggressive evaluation of any
+// expression it can figure out at compile time, it's sometimes
+// surprising how few places actually need the keyword.
diff --git a/patches/patches/074_comptime9.patch b/patches/patches/074_comptime9.patch
new file mode 100644
index 0000000..4f979fe
--- /dev/null
+++ b/patches/patches/074_comptime9.patch
@@ -0,0 +1,4 @@
+42c42
+< fn makeLlamas(count: usize) [count]u8 {
+---
+> fn makeLlamas(comptime count: usize) [count]u8 {