From fe3630d0166f04be2342f67bfba598ca3bafafad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Arrufat?= <1671644+arrufat@users.noreply.github.com> Date: Thu, 31 Dec 2020 01:02:37 +0900 Subject: [PATCH 1/3] update zig example to build with zig 0.7.1 I didn't know about this repository (I posted this change before as a YouTube comment) --- comptime.zig | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/comptime.zig b/comptime.zig index 788e419..0d2cdfb 100644 --- a/comptime.zig +++ b/comptime.zig @@ -1,4 +1,3 @@ -const TypeId = @import("builtin").TypeId; const std = @import("std"); const assert = std.debug.assert; const trait = std.meta.trait; @@ -19,19 +18,19 @@ const Point2 = struct { } }; -fn isFloatVec(comptime Type: type) bool { - if (!comptime trait.isContainer(Type)) return false; - if (!comptime trait.hasFn("at")(Type)) return false; - if (!comptime trait.hasFn("len")(Type)) return false; - if (!comptime @hasDecl(Type, "Child")) return false; - if (!comptime @hasDecl(Type, "Size")) return false; - if (!comptime @typeId(Type.Child) == TypeId.Float) return false; - if (!comptime @typeOf(Type.len).ReturnType == Type.Size) return false; +fn isFloatVec(comptime T: type) bool { + if (!comptime trait.isContainer(T)) return false; + if (!comptime trait.hasFn("at")(T)) return false; + if (!comptime trait.hasFn("len")(T)) return false; + if (!comptime @hasDecl(T, "Child")) return false; + if (!comptime @hasDecl(T, "Size")) return false; + if (!comptime @typeInfo(T.Child) == .Float) return false; + if (!comptime @typeInfo(@TypeOf(T.len)).Fn.return_type == T.Size) return false; return true; } -fn norm(vec: var) @typeOf(vec).Child { - const Vec = @typeOf(vec); +fn norm(vec: anytype) @TypeOf(vec).Child { + const Vec = @TypeOf(vec); assert(isFloatVec(Vec)); // Check empty first to avoid adding 0 and achieve zero cost. if (vec.len() == 0) { @@ -52,6 +51,6 @@ export fn norm2(x: f32, y: f32) f32 { } pub fn main() void { - std.debug.warn("hey: {} {}\n", isFloatVec(Point2), isFloatVec(i32)); - std.debug.warn("norm: {}\n", norm2(3, 4)); + std.debug.warn("hey: {} {}\n", .{ isFloatVec(Point2), isFloatVec(i32) }); + std.debug.warn("norm: {}\n", .{norm2(3, 4)}); } From 9e2ac3b91f743bca5f729f7ce2b6d9a1927e31f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Arrufat?= Date: Tue, 18 Jul 2023 12:09:36 +0900 Subject: [PATCH 2/3] Update to latest 0.11.0 nightly --- comptime.zig | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/comptime.zig b/comptime.zig index 0d2cdfb..4f54b9d 100644 --- a/comptime.zig +++ b/comptime.zig @@ -13,7 +13,7 @@ const Point2 = struct { return if (i == 0) self.x else self.y; } - pub fn len(self: Point2) Size { + pub fn len(_: Point2) Size { return 2; } }; @@ -51,6 +51,6 @@ export fn norm2(x: f32, y: f32) f32 { } pub fn main() void { - std.debug.warn("hey: {} {}\n", .{ isFloatVec(Point2), isFloatVec(i32) }); - std.debug.warn("norm: {}\n", .{norm2(3, 4)}); + std.debug.print("hey: {} {}\n", .{ isFloatVec(Point2), isFloatVec(i32) }); + std.debug.print("norm: {}\n", .{norm2(3, 4)}); } From 0b40039e3859527d184ade398a63b341b9ca08bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Arrufat?= Date: Tue, 18 Jul 2023 12:11:24 +0900 Subject: [PATCH 3/3] Use same name for Type --- comptime.zig | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/comptime.zig b/comptime.zig index 4f54b9d..4abc220 100644 --- a/comptime.zig +++ b/comptime.zig @@ -18,14 +18,14 @@ const Point2 = struct { } }; -fn isFloatVec(comptime T: type) bool { - if (!comptime trait.isContainer(T)) return false; - if (!comptime trait.hasFn("at")(T)) return false; - if (!comptime trait.hasFn("len")(T)) return false; - if (!comptime @hasDecl(T, "Child")) return false; - if (!comptime @hasDecl(T, "Size")) return false; - if (!comptime @typeInfo(T.Child) == .Float) return false; - if (!comptime @typeInfo(@TypeOf(T.len)).Fn.return_type == T.Size) return false; +fn isFloatVec(comptime Type: type) bool { + if (!comptime trait.isContainer(Type)) return false; + if (!comptime trait.hasFn("at")(Type)) return false; + if (!comptime trait.hasFn("len")(Type)) return false; + if (!comptime @hasDecl(Type, "Child")) return false; + if (!comptime @hasDecl(Type, "Size")) return false; + if (!comptime @typeInfo(Type.Child) == .Float) return false; + if (!comptime @typeInfo(@TypeOf(Type.len)).Fn.return_type == Type.Size) return false; return true; }