From 5a7c4d07793e7193911372ee34195dfc64dd7e70 Mon Sep 17 00:00:00 2001 From: Dave Gauer Date: Thu, 22 Apr 2021 17:17:25 -0400 Subject: [PATCH] add 071 comptime 6 inline for --- TEMP_TODO | 4 +++ build.zig | 4 +++ exercises/065_builtins2.zig | 2 +- exercises/071_comptime6.zig | 56 +++++++++++++++++++++++++++++ patches/patches/071_comptime6.patch | 4 +++ 5 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 TEMP_TODO create mode 100644 exercises/071_comptime6.zig create mode 100644 patches/patches/071_comptime6.patch diff --git a/TEMP_TODO b/TEMP_TODO new file mode 100644 index 0000000..9b74397 --- /dev/null +++ b/TEMP_TODO @@ -0,0 +1,4 @@ +071 - inline for - loop through struct fields +072 - inline while (see lib/std/fmt.zig) +073 - comptime block +074 - quiz 8 - revisit 058_quiz7 and make those connections programatically? diff --git a/build.zig b/build.zig index fa70eb3..e234c9c 100644 --- a/build.zig +++ b/build.zig @@ -353,6 +353,10 @@ const exercises = [_]Exercise{ .output = "\"Quack.\" ducky1: true, \"Squeek!\" ducky2: true, ducky3: false", .hint = "Have you kept the wizard hat on?", }, + .{ + .main_file = "071_comptime6.zig", + .output = "Narcissus has room in his heart for: me myself.", + }, }; /// Check the zig version to make sure it can compile the examples properly. diff --git a/exercises/065_builtins2.zig b/exercises/065_builtins2.zig index 9667b76..ef41797 100644 --- a/exercises/065_builtins2.zig +++ b/exercises/065_builtins2.zig @@ -90,7 +90,7 @@ pub fn main() void { // A StructFields array const fields = @typeInfo(Narcissus).Struct.fields; - // 'fields' is an array of StructFields. Here's the declaration: + // 'fields' is a slice of StructFields. Here's the declaration: // // pub const StructField = struct { // name: []const u8, diff --git a/exercises/071_comptime6.zig b/exercises/071_comptime6.zig new file mode 100644 index 0000000..2efba2b --- /dev/null +++ b/exercises/071_comptime6.zig @@ -0,0 +1,56 @@ +// +// There have been several instances where it would have been +// nice to use loops in our programs, but we couldn't because the +// things we were trying to do could only be done at compile +// time. We ended up having to do those things MANUALLY, like +// NORMAL people. Bah! We are PROGRAMMERS! The computer should be +// doing this work. +// +// An 'inline for' is performed at compile time, allowing you to +// programatically loop through a series of items in situations +// like those mentioned above where a regular runtime 'for' loop +// wouldn't be allowed: +// +// inline for (.{ u8, u16, u32, u64 }) |T| { +// print("{} ", .{@typeInfo(T).Int.bits}); +// } +// +// In the above example, we're looping over a list of types, +// which are available only at compile time. +// +const print = @import("std").debug.print; + +// Remember Narcissus from exercise 065 where we used builtins +// for reflection? He's back and loving it. +const Narcissus = struct { + me: *Narcissus = undefined, + myself: *Narcissus = undefined, + echo: void = undefined, +}; + +pub fn main() void { + var narcissus: Narcissus = Narcissus {}; + + print("Narcissus has room in his heart for:", .{}); + + // Last time we examined the Narcissus struct, we had to + // manually access each of the three fields. Our 'if' + // statement was repeated three times almost verbatim. Yuck! + // + // Please use an 'inline for' to implement the block below + // for each field in the slice 'fields'! + + const fields = @typeInfo(Narcissus).Struct.fields; + + ??? { + if (field.field_type != void) { + print(" {s}", .{field.name}); + } + } + + // Once you've got that, go back and take a look at exercise + // 065 and compare what you've written to the abomination we + // had there! + + print(".\n", .{}); +} diff --git a/patches/patches/071_comptime6.patch b/patches/patches/071_comptime6.patch new file mode 100644 index 0000000..9f11567 --- /dev/null +++ b/patches/patches/071_comptime6.patch @@ -0,0 +1,4 @@ +45c45 +< ??? { +--- +> inline for (fields) |field| {