diff options
author | Dave Gauer <dave@ratfactor.com> | 2021-04-09 14:41:25 -0400 |
---|---|---|
committer | Dave Gauer <dave@ratfactor.com> | 2021-04-09 14:41:25 -0400 |
commit | 0d7c14a4b0edfe07c4d81e592101009e4432286f (patch) | |
tree | 9b11b1f6f4aca519453e1536e3dafe9334f689d9 /exercises/061_coercions.zig | |
parent | dd31256b88f07146b97ddc8dd1181dfc8f4644e3 (diff) | |
download | ziglings-0d7c14a4b0edfe07c4d81e592101009e4432286f.tar.gz ziglings-0d7c14a4b0edfe07c4d81e592101009e4432286f.tar.bz2 ziglings-0d7c14a4b0edfe07c4d81e592101009e4432286f.tar.xz ziglings-0d7c14a4b0edfe07c4d81e592101009e4432286f.zip |
added ex061 coercions
Diffstat (limited to 'exercises/061_coercions.zig')
-rw-r--r-- | exercises/061_coercions.zig | 78 |
1 files changed, 78 insertions, 0 deletions
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]}); +} |