diff options
author | Dave Gauer <dave@ratfactor.com> | 2021-04-21 09:47:16 -0400 |
---|---|---|
committer | Dave Gauer <dave@ratfactor.com> | 2021-04-21 09:47:16 -0400 |
commit | 751b303362f98d8b62364d43cffd4d93034d4e10 (patch) | |
tree | ccc63767c201f1bc4c5ddbb337cadd04f9e76de4 | |
parent | ec543ff49b5cbdc1c8fa3268a155a794ee30af49 (diff) | |
download | ziglings-751b303362f98d8b62364d43cffd4d93034d4e10.tar.gz ziglings-751b303362f98d8b62364d43cffd4d93034d4e10.tar.bz2 ziglings-751b303362f98d8b62364d43cffd4d93034d4e10.tar.xz ziglings-751b303362f98d8b62364d43cffd4d93034d4e10.zip |
add ex069 comptime 4
-rw-r--r-- | README.md | 7 | ||||
-rw-r--r-- | build.zig | 9 | ||||
-rw-r--r-- | exercises/069_comptime4.zig | 54 | ||||
-rw-r--r-- | patches/patches/069_comptime4.patch | 6 |
4 files changed, 74 insertions, 2 deletions
@@ -41,7 +41,7 @@ Verify the installation and build number of `zig` like so: ```bash $ zig version -0.8.0-dev.1065+xxxxxxxxx +0.8.0-dev.1983+xxxxxxxxx ``` Clone this repository with Git: @@ -71,6 +71,11 @@ Once you have a build of the Zig compiler that works with Ziglings, they'll continue to work together. But keep in mind that if you update one, you may need to also update the other. +### Version Changes + +* 2021-04-21 0.8.0-dev.1983 - std.fmt.format() 'any' format string +* 2021-02-12 0.8.0-dev.1065 - std.fmt.format() 's' (string) format string + ## Advanced Usage It can be handy to check just a single exercise or _start_ from a single @@ -344,6 +344,10 @@ const exercises = [_]Exercise{ .main_file = "068_comptime3.zig", .output = "Minnow (1:32, 4 x 2)\nShark (1:16, 8 x 5)\nWhale (1:1, 143 x 95)\n", }, + .{ + .main_file = "069_comptime4.zig", + .output = "s1={ 1, 2, 3 }, s2={ 1, 2, 3, 4, 5 }, s3={ 1, 2, 3, 4, 5, 6, 7 }", + }, }; /// Check the zig version to make sure it can compile the examples properly. @@ -353,7 +357,10 @@ fn checkVersion() bool { return false; } - const needed_version = std.SemanticVersion.parse("0.8.0-dev.1065") catch unreachable; + // When changing this version, be sure to also update README.md in two places: + // 1) Getting Started + // 2) Version Changes + const needed_version = std.SemanticVersion.parse("0.8.0-dev.1983") catch unreachable; const version = std.builtin.zig_version; const order = version.order(needed_version); return order != .lt; diff --git a/exercises/069_comptime4.zig b/exercises/069_comptime4.zig new file mode 100644 index 0000000..d2aec5e --- /dev/null +++ b/exercises/069_comptime4.zig @@ -0,0 +1,54 @@ +// +// One of the more common uses of 'comptime' function parameters is +// passing a type to a function: +// +// fn foo(comptime MyType: type) { ... } +// +// In fact, types are ONLY available at compile time, so the +// 'comptime' keyword is required here. +// +// Please take a moment put on the wizard hat which has been +// provided for you. We're about to use this ability to implement +// a generic function. +// +const print = @import("std").debug.print; + +pub fn main() void { + // Here we declare arrays of three different types and sizes + // at compile time from a function call. Neat! + const s1 = makeSequence(u8, 3); // creates a [3]u8 + const s2 = makeSequence(u32, 5); // creates a [5]u32 + const s3 = makeSequence(i64, 7); // creates a [7]i64 + + print("s1={any}, s2={any}, s3={any}\n", .{s1, s2, s3}); +} + +// This function is pretty wild because it executes at runtime +// and is part of the final compiled program. The function is +// compiled with unchanging data sizes and types. +// +// And yet it ALSO allows for different sizes and types. This +// seems paradoxical. How could both things be true? +// +// To accomplish this, the Zig compiler actually generates a +// separate copy of the function for every size/type combination! +// So in this case, three different functions will be generated +// for you, each with machine code that handles that specific +// data size and type. +// +// Please fix this function so that the 'size' parameter: +// +// 1) Is guaranteed to be known at compile time. +// 2) Sets the size of the array of type T (which is the +// sequence we're creating and returning). +// +fn makeSequence(comptime T: type, ??? size: usize) [???]T { + var sequence: [???]T = undefined; + var i: usize = 0; + + while (i < size) : (i += 1) { + sequence[i] = @intCast(T, i) + 1; + } + + return sequence; +} diff --git a/patches/patches/069_comptime4.patch b/patches/patches/069_comptime4.patch new file mode 100644 index 0000000..3d662a7 --- /dev/null +++ b/patches/patches/069_comptime4.patch @@ -0,0 +1,6 @@ +45,46c45,46 +< fn makeSequence(comptime T: type, ??? size: usize) [???]T { +< var sequence: [???]T = undefined; +--- +> fn makeSequence(comptime T: type, comptime size: usize) [size]T { +> var sequence: [size]T = undefined; |