3 ms·
This proposal is different than how zig handles generic functions because it only emits one symbol and function body for each function. In zig, genericMax([]i3
by pfg_ 2y ago
This proposal is different than how zig handles generic functions because it only emits one symbol and function body for each function.
In zig, genericMax([]i32) would emit seperate code to genericMax([]i16). This proposal has both of those functions backed by the same symbol and machine code but it requires a bunch of extra arguments, virtual function calls, and manualy offsetting indices into arrays. You could use zig to do the same with some effort.
- pfg_ 2y agofn genericReduceTypeErased( size: usize, total: [*]u8, list: []const u8, reducer: *const fn(total: [*]u8, current: [*]const u8) callconv(.C) void, ) void { for(0..@divExact(list.len, size)) |i| { const itm = &list[i \* size]; reducer(total, @ptrCast(itm)); } } inline fn genericReduce( comptime T: type, total: *T, list: []const T, reducer: *const fn(total: *T, current: *const T) callconv(.C) void, ) void { genericReduceTypeErased( @sizeOf(T), @ptrCast(total), std.mem.sliceAsBytes(list), @ptrCast(reducer) ); } The proposal is basically syntax sugar for making a typed version of the first function in C