From 96f5b425e3a466c79b9759641d2440a29f4fe060 Mon Sep 17 00:00:00 2001 From: Dave Gauer Date: Fri, 9 Apr 2021 14:41:25 -0400 Subject: [PATCH] added ex061 coercions --- build.zig | 4 ++ exercises/061_coercions.zig | 78 +++++++++++++++++++++++++++++ patches/patches/061_coercions.patch | 4 ++ 3 files changed, 86 insertions(+) create mode 100644 exercises/061_coercions.zig create mode 100644 patches/patches/061_coercions.patch diff --git a/build.zig b/build.zig index daf065f..fa27a1e 100644 --- a/build.zig +++ b/build.zig @@ -310,6 +310,10 @@ const exercises = [_]Exercise{ .main_file = "060_floats.zig", .output = "Shuttle liftoff weight: 1995796kg", }, + .{ + .main_file = "061_coercions.zig", + .output = "Letter: A", + }, }; /// Check the zig version to make sure it can compile the examples properly. diff --git a/exercises/061_coercions.zig b/exercises/061_coercions.zig new file mode 100644 index 0000000..043684e --- /dev/null +++ b/exercises/061_coercions.zig @@ -0,0 +1,78 @@ +// +// It'll only take us a moment to learn the Zig type coercion +// rules because they're quite logical. +// +// 1. Types can always be made _more_ restrictive. +// +// var foo: u8 = 5; +// var p1: *u8 = &foo; +// var p2: *const u8 = p1; // mutable to immutable +// +// 2. Numeric types can coerce to _larger_ types. +// +// var n1: u8 = 5; +// var n2: u16 = n8; // integer "widening" +// +// var n3: f16 = 42.0; +// var n4: f32 = n3; // float "widening" +// +// 3. Single-item pointers to arrays coerce to slices and +// many-item pointers. +// +// const arr: [3]u8 = [3]u8{5, 6, 7}; +// const s: []const u8 = &arr; // to slice +// const p: [*]const u8 = &arr; // to many-item pointer +// +// 4. Single-item mutable pointers can coerce to single-item +// pointers pointing to an array of length 1. (Interesting!) +// +// var five: u8 = 5; +// var a_five: *[1]u8 = &five; +// +// 5. Payload types and null coerce to optionals. +// +// var num: u8 = 5; +// var maybe_num: ?u8 = num; // payload type +// maybe_num = null; // null +// +// 6. Payload types and errors coerce to error unions. +// +// const MyError = error{Argh}; +// var char: u8 = 'x'; +// var char_or_die: MyError!u8 = char; // payload type +// char_or_die = MyError.Argh; // error +// +// 7. 'undefined' coerces to any type (or it wouldn't work!) +// +// 8. Compile-time numbers coerce to compatible types. +// +// Just about every single exercise program has had an example +// of this, but a full and proper explanation is coming your +// way soon in the third-eye-opening subject of comptime. +// +// 9. Tagged unions coerce to the current tagged enum. +// +// 10. Enums coerce to a tagged union when that tagged field is a +// a zero-length type that has only one value (like void). +// +// 11. Zero-bit types (like void) can be coerced into single-item +// pointers. +// +// The last three are fairly esoteric, but you're more than +// welcome to read more about them in the official Zig language +// documentation and write your own experiments. + +const print = @import("std").debug.print; + +pub fn main() void { + var letter: u8 = 'A'; + + const my_letter: ??? = &letter; + // ^^^^^^^ + // Your type here. + // Must coerce from &letter (which is a *u8). + // Hint: Use coercion Rules 4 and 5. + + // When it's right, this will work: + print("Letter: {u}\n", .{my_letter.?.*[0]}); +} diff --git a/patches/patches/061_coercions.patch b/patches/patches/061_coercions.patch new file mode 100644 index 0000000..4661154 --- /dev/null +++ b/patches/patches/061_coercions.patch @@ -0,0 +1,4 @@ +70c70 +< const my_letter: ??? = &letter; +--- +> const my_letter: ?*[1]u8 = &letter;