diff options
author | Dave Gauer <dave@ratfactor.com> | 2021-05-09 13:24:25 -0400 |
---|---|---|
committer | Dave Gauer <dave@ratfactor.com> | 2021-05-09 13:24:25 -0400 |
commit | da3ec19000ee124371793ec3e846e5c2c1b164c2 (patch) | |
tree | d74e9b14f2f4ada71270ba63b272848939551975 | |
parent | 2ad8de54150137244dc1d1b93bb3a75e850ee2b9 (diff) | |
download | ziglings-da3ec19000ee124371793ec3e846e5c2c1b164c2.tar.gz ziglings-da3ec19000ee124371793ec3e846e5c2c1b164c2.tar.bz2 ziglings-da3ec19000ee124371793ec3e846e5c2c1b164c2.tar.xz ziglings-da3ec19000ee124371793ec3e846e5c2c1b164c2.zip |
add ex080 anonymous structs 1
-rw-r--r-- | build.zig | 4 | ||||
-rw-r--r-- | exercises/080_anonymous_structs.zig | 76 | ||||
-rw-r--r-- | patches/patches/080_anonymous_structs.patch | 8 |
3 files changed, 88 insertions, 0 deletions
@@ -396,6 +396,10 @@ const exercises = [_]Exercise{ .output = "Sweet freedom: 55, false.", .hint = "Help us, Zig Programmer, you're our only hope!", }, + .{ + .main_file = "080_anonymous_structs.zig", + .output = "[Circle(i32): 25,70,15] [Circle(f32): 25.2,71.0,15.7]", + }, }; /// Check the zig version to make sure it can compile the examples properly. diff --git a/exercises/080_anonymous_structs.zig b/exercises/080_anonymous_structs.zig new file mode 100644 index 0000000..bbf3690 --- /dev/null +++ b/exercises/080_anonymous_structs.zig @@ -0,0 +1,76 @@ +// +// Struct types are always "anonymous" until we give them a name: +// +// struct {}; +// +// So far, we've been giving struct types a name like so: +// +// const Foo = struct {}; +// +// * The value of @typeName(Foo) is "Foo". +// +// A struct is also given a name when you return it from a +// function: +// +// fn Bar() type { +// return struct {}; +// } +// +// const MyBar = Bar(); // store the struct type +// const bar = Bar() {}; // create instance of the struct +// +// * The value of @typeName(Bar()) is "Bar()". +// * The value of @typeName(MyBar) is "Bar()". +// * The value of @typeName(@TypeOf(bar)) is "Bar()". +// +// You can also have completely anonymous structs. The value +// of @typeName(struct {}) is "struct:<position in source>". +// +const print = @import("std").debug.print; + +// This function creates a generic data structure by returning an +// anonymous struct type (which will no longer be anonymous AFTER +// it's returned from the function). +fn Circle(comptime T: type) type { + return struct { + center_x: T, + center_y: T, + radius: T, + }; +} + +pub fn main() void { + // + // See if you can complete these two variable initialization + // expressions to create instances of circle struct types + // which can hold these values: + // + // * circle1 should hold i32 integers + // * circle2 should hold f32 floats + // + var circle1 = ??? { + .center_x = 25, + .center_y = 70, + .radius = 15, + }; + + var circle2 = ??? { + .center_x = 25.234, + .center_y = 70.999, + .radius = 15.714, + }; + + print("[{s}: {},{},{}] ", .{ + @typeName(@TypeOf(circle1)), + circle1.center_x, + circle1.center_y, + circle1.radius, + }); + + print("[{s}: {d:.1},{d:.1},{d:.1}]\n", .{ + @typeName(@TypeOf(circle2)), + circle2.center_x, + circle2.center_y, + circle2.radius, + }); +} diff --git a/patches/patches/080_anonymous_structs.patch b/patches/patches/080_anonymous_structs.patch new file mode 100644 index 0000000..c9edecf --- /dev/null +++ b/patches/patches/080_anonymous_structs.patch @@ -0,0 +1,8 @@ +51c51 +< var circle1 = ??? { +--- +> var circle1 = Circle(i32) { +57c57 +< var circle2 = ??? { +--- +> var circle2 = Circle(f32) { |