aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Gauer <[email protected]>2022-08-29 20:07:48 -0400
committerDave Gauer <[email protected]>2022-08-29 20:10:26 -0400
commit1691b22c1b59c7065666a49b212df665f18792a3 (patch)
tree104df8ebd9818e23615c9a604e691bd54c8257f2
parent4eaef5fae41f3c15b2e45240c9a57d2c2740c5e3 (diff)
downloadziglings-1691b22c1b59c7065666a49b212df665f18792a3.tar.xz
ziglings-1691b22c1b59c7065666a49b212df665f18792a3.zip
Ex 080: Strip filename from @typeName output to address #130
-rw-r--r--exercises/080_anonymous_structs.zig15
-rw-r--r--patches/patches/080_anonymous_structs.patch26
2 files changed, 30 insertions, 11 deletions
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/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,