From b86230d1609d0eb8a6356f6e988c3b06d1e7342f Mon Sep 17 00:00:00 2001 From: Dave Gauer Date: Sun, 9 May 2021 14:25:51 -0400 Subject: add ex081 anon structs 2 --- build.zig | 4 +++ exercises/081_anonymous_structs2.zig | 47 ++++++++++++++++++++++++++++ patches/patches/081_anonymous_structs2.patch | 8 +++++ 3 files changed, 59 insertions(+) create mode 100644 exercises/081_anonymous_structs2.zig create mode 100644 patches/patches/081_anonymous_structs2.patch diff --git a/build.zig b/build.zig index 7abb48e..0a7b8b8 100644 --- a/build.zig +++ b/build.zig @@ -400,6 +400,10 @@ const exercises = [_]Exercise{ .main_file = "080_anonymous_structs.zig", .output = "[Circle(i32): 25,70,15] [Circle(f32): 25.2,71.0,15.7]", }, + .{ + .main_file = "081_anonymous_structs2.zig", + .output = "x:205 y:187 radius:12", + }, }; /// Check the zig version to make sure it can compile the examples properly. diff --git a/exercises/081_anonymous_structs2.zig b/exercises/081_anonymous_structs2.zig new file mode 100644 index 0000000..6871cc1 --- /dev/null +++ b/exercises/081_anonymous_structs2.zig @@ -0,0 +1,47 @@ +// +// An anonymous struct value LITERAL (not to be confused with a +// struct TYPE) uses '.{}' syntax: +// +// .{ +// .center_x = 15, +// .center_y = 12, +// .radius = 6, +// } +// +// These literals are always evaluated entirely at compile-time. +// The example above could be coerced into the i32 variant of the +// "circle struct" from the last exercise. +// +// Or you can let them remain entirely anonymous as in this +// example: +// +// fn bar(foo: anytype) void { +// print("a:{} b:{}\n", .{foo.a, foo.b}); +// } +// +// bar(.{ +// .a = true, +// .b = false, +// }); +// +// The example above prints "a:true b:false". +// +const print = @import("std").debug.print; + +pub fn main() void { + printCircle(.{ + .center_x = @as(u32, 205), + .center_y = @as(u32, 187), + .radius = @as(u32, 12), + }); +} + +// Please complete this function which prints an anonymous struct +// representing a circle. +fn printCircle(???) void { + print("x:{} y:{} radius:{}\n", .{ + circle.center_x, + circle.centaur_y, + circle.radius, + }); +} diff --git a/patches/patches/081_anonymous_structs2.patch b/patches/patches/081_anonymous_structs2.patch new file mode 100644 index 0000000..b9890d6 --- /dev/null +++ b/patches/patches/081_anonymous_structs2.patch @@ -0,0 +1,8 @@ +41c41 +< fn printCircle(???) void { +--- +> fn printCircle(circle: anytype) void { +44c44 +< circle.centaur_y, +--- +> circle.center_y, -- cgit v1.2.3-ZIG