summaryrefslogtreecommitdiffstatshomepage
path: root/exercises
diff options
context:
space:
mode:
authorDave Gauer <dave@ratfactor.com>2021-04-21 09:47:16 -0400
committerDave Gauer <dave@ratfactor.com>2021-04-21 09:47:16 -0400
commitbe2b98f4047d81804d0d65e73a25828420e6a26c (patch)
treeccc63767c201f1bc4c5ddbb337cadd04f9e76de4 /exercises
parent17a22adce10e016489feb243587f0bf80cdd2f76 (diff)
add ex069 comptime 4
Diffstat (limited to 'exercises')
-rw-r--r--exercises/069_comptime4.zig54
1 files changed, 54 insertions, 0 deletions
diff --git a/exercises/069_comptime4.zig b/exercises/069_comptime4.zig
new file mode 100644
index 0000000..d2aec5e
--- /dev/null
+++ b/exercises/069_comptime4.zig
@@ -0,0 +1,54 @@
+//
+// One of the more common uses of 'comptime' function parameters is
+// passing a type to a function:
+//
+// fn foo(comptime MyType: type) { ... }
+//
+// In fact, types are ONLY available at compile time, so the
+// 'comptime' keyword is required here.
+//
+// Please take a moment put on the wizard hat which has been
+// provided for you. We're about to use this ability to implement
+// a generic function.
+//
+const print = @import("std").debug.print;
+
+pub fn main() void {
+ // Here we declare arrays of three different types and sizes
+ // at compile time from a function call. Neat!
+ const s1 = makeSequence(u8, 3); // creates a [3]u8
+ const s2 = makeSequence(u32, 5); // creates a [5]u32
+ const s3 = makeSequence(i64, 7); // creates a [7]i64
+
+ print("s1={any}, s2={any}, s3={any}\n", .{s1, s2, s3});
+}
+
+// This function is pretty wild because it executes at runtime
+// and is part of the final compiled program. The function is
+// compiled with unchanging data sizes and types.
+//
+// And yet it ALSO allows for different sizes and types. This
+// seems paradoxical. How could both things be true?
+//
+// To accomplish this, the Zig compiler actually generates a
+// separate copy of the function for every size/type combination!
+// So in this case, three different functions will be generated
+// for you, each with machine code that handles that specific
+// data size and type.
+//
+// Please fix this function so that the 'size' parameter:
+//
+// 1) Is guaranteed to be known at compile time.
+// 2) Sets the size of the array of type T (which is the
+// sequence we're creating and returning).
+//
+fn makeSequence(comptime T: type, ??? size: usize) [???]T {
+ var sequence: [???]T = undefined;
+ var i: usize = 0;
+
+ while (i < size) : (i += 1) {
+ sequence[i] = @intCast(T, i) + 1;
+ }
+
+ return sequence;
+}