From 208aa3db7b4a8352b8c42cdcc3f7f7398029af2f Mon Sep 17 00:00:00 2001 From: Dave Gauer Date: Mon, 29 Aug 2022 20:07:48 -0400 Subject: Ex 080: Strip filename from @typeName output to address #130 --- exercises/080_anonymous_structs.zig | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'exercises/080_anonymous_structs.zig') 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 ".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. -- cgit v1.2.3-ZIG