From 4b41b0a5c0f0a9092e09099184b9493e464e31a8 Mon Sep 17 00:00:00 2001 From: Ahmed Date: Tue, 12 Mar 2024 21:33:37 +0000 Subject: [PATCH] 069_comptime4: replace `comptime T: type` => `T: type` and refactor a bit accordingly - replaced instances of `comptime T: type` with `T: type` (080_anonymous_structs) - left 065_builtins2.zig: 27:// 2. @typeInfo(comptime T: type) @import("std").builtin.Type see [do not enforce function parameters to be marked comptime if only called at comptime](https://github.com/ziglang/zig/pull/18331) --- exercises/069_comptime4.zig | 10 ++++------ exercises/080_anonymous_structs.zig | 2 +- patches/patches/069_comptime4.patch | 4 ++-- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/exercises/069_comptime4.zig b/exercises/069_comptime4.zig index e090bb3..4afbddf 100644 --- a/exercises/069_comptime4.zig +++ b/exercises/069_comptime4.zig @@ -1,11 +1,9 @@ // -// One of the more common uses of 'comptime' function parameters is -// passing a type to a function: +// In Zig types are first class values // -// fn foo(comptime MyType: type) void { ... } +// fn foo(MyType: type) void { ... } // -// In fact, types are ONLY available at compile time, so the -// 'comptime' keyword is required here. +// Types are resolved at compile time. // // Please take a moment to put on the wizard hat which has been // provided for you. We're about to use this ability to implement @@ -42,7 +40,7 @@ pub fn main() void { // 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 { +fn makeSequence(T: type, ??? size: usize) [???]T { var sequence: [???]T = undefined; var i: usize = 0; diff --git a/exercises/080_anonymous_structs.zig b/exercises/080_anonymous_structs.zig index 55dedd4..333802c 100644 --- a/exercises/080_anonymous_structs.zig +++ b/exercises/080_anonymous_structs.zig @@ -31,7 +31,7 @@ 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 { +fn Circle(T: type) type { return struct { center_x: T, center_y: T, diff --git a/patches/patches/069_comptime4.patch b/patches/patches/069_comptime4.patch index 2b61673..1ba5ffa 100644 --- a/patches/patches/069_comptime4.patch +++ b/patches/patches/069_comptime4.patch @@ -4,9 +4,9 @@ // 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 { +-fn makeSequence(T: type, ??? size: usize) [???]T { - var sequence: [???]T = undefined; -+fn makeSequence(comptime T: type, comptime size: usize) [size]T { ++fn makeSequence(T: type, comptime size: usize) [size]T { + var sequence: [size]T = undefined; var i: usize = 0;