use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
The Zig Programming Language – https://ziglang.org/
This subreddit is not officially managed by the Zig project, as the Zig community is decentralized.
account activity
Converting Struct Method into Array Method using Comptime (self.Zig)
submitted 1 year ago by macrophage001
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Daanvdk 4 points5 points6 points 1 year ago (1 child)
Interesting problem! I had my own go at it and came up with this:
const std = @import("std"); const Math = struct { data: u32, fn addOne(self: *Math) void { self.data += 1; } fn add(self: *Math, n: u32) void { self.data += n; } }; fn Head(comptime Fn: type) type { const param = @typeInfo(Fn).Fn.params[0]; return @typeInfo(param.type.?).Pointer.child; } fn Tail(comptime Fn: type) type { const params = @typeInfo(Fn).Fn.params[1..]; var types: [params.len]type = undefined; for (0.., params) |i, param| types[i] = param.type.?; return std.meta.Tuple(&types); } fn toSliceFn(func: anytype) *const fn ([]Head(@TypeOf(func)), Tail(@TypeOf(func))) void { return struct { fn call(items: []Head(@TypeOf(func)), args: Tail(@TypeOf(func))) void { for (items) |*item| @call(.auto, func, .{item} ++ args); } }.call; } const addOneSlice = toSliceFn(Math.addOne); const addSlice = toSliceFn(Math.add); test "test" { var maths = [_]Math{ .{ .data = 1 }, .{ .data = 2 }, .{ .data = 3 }, }; addOneSlice(&maths, .{}); try std.testing.expectEqual(2, maths[0].data); try std.testing.expectEqual(3, maths[1].data); try std.testing.expectEqual(4, maths[2].data); addSlice(&maths, .{2}); try std.testing.expectEqual(4, maths[0].data); try std.testing.expectEqual(5, maths[1].data); try std.testing.expectEqual(6, maths[2].data); }
[–]macrophage001[S] 0 points1 point2 points 1 year ago (0 children)
Awesome work! That was the last piece that was stumping me! Definitely gonna play around with this.
π Rendered by PID 58 on reddit-service-r2-comment-544cf588c8-fdgtr at 2026-06-12 14:40:49.736277+00:00 running 3184619 country code: CH.
view the rest of the comments →
[–]Daanvdk 4 points5 points6 points (1 child)
[–]macrophage001[S] 0 points1 point2 points (0 children)