diff options
-rw-r--r-- | exercises/065_builtins2.zig | 44 | ||||
-rw-r--r-- | exercises/080_anonymous_structs.zig | 15 | ||||
-rw-r--r-- | patches/patches/065_builtins2.patch | 65 | ||||
-rw-r--r-- | patches/patches/080_anonymous_structs.patch | 26 |
4 files changed, 102 insertions, 48 deletions
diff --git a/exercises/065_builtins2.zig b/exercises/065_builtins2.zig index d50b6f0..a0f9160 100644 --- a/exercises/065_builtins2.zig +++ b/exercises/065_builtins2.zig @@ -40,12 +40,12 @@ // (Notice how the two functions which return types start with // uppercase letters? This is a standard naming practice in Zig.) // -const print = import(std).debug.print; // Oops! +const print = @import("std").debug.print; const Narcissus = struct { me: *Narcissus = undefined, myself: *Narcissus = undefined, - echo: void = undefined, + echo: void = undefined, // Alas, poor Echo! fn fetchTheMostBeautifulType() type { return @This(); @@ -57,20 +57,26 @@ pub fn main() void { // Oops! We cannot leave the 'me' and 'myself' fields // undefined. Please set them here: - ??? = &narcissus; - ??? = &narcissus; + narcissus.me = &narcissus; + narcissus.??? = ???; // This determines a "peer type" from three separate // references (they just happen to all be the same object). - const T1 = @TypeOf(narcissus, narcissus.me.*, narcissus.myself.*); + const Type1 = @TypeOf(narcissus, narcissus.me.*, narcissus.myself.*); // Oh dear, we seem to have done something wrong when calling - // this function. It is namespaced to the struct, but doesn't - // use the method syntax (there's no self parameter). Please - // fix this call: - const T2 = narcissus.fetchTheMostBeautifulType(); + // this function. We called it as a method, which would work + // if it had a self parameter. But it doesn't. (See above.) + // + // The fix for this is very subtle, but it makes a big + // difference! + const Type2 = narcissus.fetchTheMostBeautifulType(); - print("A {} loves all {}es. ", .{ T1, T2 }); + // Now we print a pithy statement about Narcissus. + print("A {s} loves all {s}es. ", .{ + maximumNarcissism(Type1), + maximumNarcissism(Type2), + }); // His final words as he was looking in // those waters he habitually watched @@ -121,7 +127,23 @@ pub fn main() void { // Alas, we can't use a regular 'for' loop here because // 'fields' can only be evaluated at compile time. It seems // like we're overdue to learn about this "comptime" stuff, - // doesn't it? :-) + // doesn't it? Don't worry, we'll get there. print(".\n", .{}); } + +// NOTE: This exercise did not originally include the function below. +// But a change after Zig 0.10.0 added the source file name to the +// type. "Narcissus" became "065_builtins2.Narcissus". +// +// To fix this, I've added this function to strip the filename from +// the front of the type name in the dumbest way possible. (It returns +// a slice of the type name starting at character 14 (assuming +// single-byte characters). +// +// We'll be seeing @typeName again in Exercise 070. For now, you can +// see that it takes a Type and returns a u8 "string". +fn maximumNarcissism(myType: anytype) []const u8 { + // Turn '065_builtins2.Narcissus' into 'Narcissus' + return @typeName(myType)[14..]; +} diff --git a/exercises/080_anonymous_structs.zig b/exercises/080_anonymous_structs.zig index bbf3690..0ca8f0c 100644 --- a/exercises/080_anonymous_structs.zig +++ b/exercises/080_anonymous_structs.zig @@ -7,7 +7,7 @@ // // const Foo = struct {}; // -// * The value of @typeName(Foo) is "Foo". +// * The value of @typeName(Foo) is "<filename>.Foo". // // A struct is also given a name when you return it from a // function: @@ -61,16 +61,25 @@ pub fn main() void { }; print("[{s}: {},{},{}] ", .{ - @typeName(@TypeOf(circle1)), + stripFname(@typeName(@TypeOf(circle1))), circle1.center_x, circle1.center_y, circle1.radius, }); print("[{s}: {d:.1},{d:.1},{d:.1}]\n", .{ - @typeName(@TypeOf(circle2)), + stripFname(@typeName(@TypeOf(circle2))), circle2.center_x, circle2.center_y, circle2.radius, }); } + +// Perhaps you remember the "narcissistic fix" for the type name +// in Ex. 065? We're going to do the same thing here: use a hard- +// coded slice to return the type name. That's just so our output +// look prettier. Indulge your vanity. Programmers are beautiful. +fn stripFname(mytype: []const u8) []const u8 { + return mytype[22..]; +} +// The above would be an instant red flag in a "real" program. diff --git a/patches/patches/065_builtins2.patch b/patches/patches/065_builtins2.patch index 1c2acf5..9c1bf76 100644 --- a/patches/patches/065_builtins2.patch +++ b/patches/patches/065_builtins2.patch @@ -1,26 +1,39 @@ -43c43 -< const print = import(std).debug.print; // Oops! ---- -> const print = @import("std").debug.print; -60,61c60,61 -< ??? = &narcissus; -< ??? = &narcissus; ---- -> narcissus.me = &narcissus; -> narcissus.myself = &narcissus; -71c71 -< const T2 = narcissus.fetchTheMostBeautifulType(); ---- -> const T2 = Narcissus.fetchTheMostBeautifulType(); -106c106 -< if (fields[0].??? != void) { ---- -> if (fields[0].field_type != void) { -110c110 -< if (fields[1].??? != void) { ---- -> if (fields[1].field_type != void) { -114c114 -< if (fields[2].??? != void) { ---- -> if (fields[2].field_type != void) { +--- exercises/065_builtins2.zig ++++ answers/065_builtins2.zig +@@ -58,7 +58,7 @@ + // Oops! We cannot leave the 'me' and 'myself' fields + // undefined. Please set them here: + narcissus.me = &narcissus; +- narcissus.??? = ???; ++ narcissus.myself = &narcissus; + + // This determines a "peer type" from three separate + // references (they just happen to all be the same object). +@@ -70,7 +70,7 @@ + // + // The fix for this is very subtle, but it makes a big + // difference! +- const Type2 = narcissus.fetchTheMostBeautifulType(); ++ const Type2 = Narcissus.fetchTheMostBeautifulType(); + + // Now we print a pithy statement about Narcissus. + print("A {s} loves all {s}es. ", .{ +@@ -109,15 +109,15 @@ + // Please complete these 'if' statements so that the field + // name will not be printed if the field is of type 'void' + // (which is a zero-bit type that takes up no space at all!): +- if (fields[0].??? != void) { ++ if (fields[0].field_type != void) { + print(" {s}", .{@typeInfo(Narcissus).Struct.fields[0].name}); + } + +- if (fields[1].??? != void) { ++ if (fields[1].field_type != void) { + print(" {s}", .{@typeInfo(Narcissus).Struct.fields[1].name}); + } + +- if (fields[2].??? != void) { ++ if (fields[2].field_type != void) { + print(" {s}", .{@typeInfo(Narcissus).Struct.fields[2].name}); + } + diff --git a/patches/patches/080_anonymous_structs.patch b/patches/patches/080_anonymous_structs.patch index c9edecf..6df1890 100644 --- a/patches/patches/080_anonymous_structs.patch +++ b/patches/patches/080_anonymous_structs.patch @@ -1,8 +1,18 @@ -51c51 -< var circle1 = ??? { ---- -> var circle1 = Circle(i32) { -57c57 -< var circle2 = ??? { ---- -> var circle2 = Circle(f32) { +--- exercises/080_anonymous_structs.zig ++++ answers/080_anonymous_structs.zig +@@ -48,13 +48,13 @@ + // * circle1 should hold i32 integers + // * circle2 should hold f32 floats + // +- var circle1 = ??? { ++ var circle1 = Circle(i32){ + .center_x = 25, + .center_y = 70, + .radius = 15, + }; + +- var circle2 = ??? { ++ var circle2 = Circle(f32){ + .center_x = 25.234, + .center_y = 70.999, + .radius = 15.714, |