Re:Type-erased generic functions for C: A modest non-proposal
by anacrolix on Thursday October 03, 2024 @04:04AM
(#41728357)
Just use Zig?
[ Parent
| on HN ]
Re:Type-erased generic functions for C: A modest non-proposal
by pfg_ on Thursday October 03, 2024 @04:38AM
(#41728577)
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.
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
Re:Type-erased generic functions for C: A modest non-proposal
by WhereIsTheTruth on Thursday October 03, 2024 @06:37AM
(#41729292)
Just use D?
The point is not to use something else, but to improve what they already use
Re:Type-erased generic functions for C: A modest non-proposal
by samatman on Thursday October 03, 2024 @11:32AM
(#41731705)
And a lot of people will, but we're not taking C out back of the barn and shooting it. It's here to stay and adding some new features which make it a better language is a good idea.
I'm not convinced this specific proposal does that, but that's a separate matter. JenHeyd Meneide has a Technical Specification paper for a defer statement in C, which is visibly based on Zig's version, and I applaud that.
https://thephd.dev/just-put-raii-in-c-bro-please-bro-just-on...