News for hackers. Stuff that matters.
Re:Type-erased generic functions for C: A modest non-proposal by pfg_ on Thursday October 03, 2024 @04:57AM (#41728708)

    fn 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

There are no comments on this comment yet.