News for hackers. Stuff that matters.
Re:Implementing Generic Types in C by lukaslalinsky on Wednesday March 19, 2025 @02:15PM (#43415498)
For C with generic types, I think one should pick up Zig. It's exactly that, it's a low level language with manual memory management, but it allows you to do generic types like this without hacks.
Re:Implementing Generic Types in C by alcover on Wednesday March 19, 2025 @04:03PM (#43416653)
I often see such replies and wonder : the article isn't about choosing langs but is an exercise in C. That's the constraint.
Re:Implementing Generic Types in C by bsder on Wednesday March 19, 2025 @04:26PM (#43416902)
If C is the constraint, then why are you doing generic types?

If you're using C, use bloody C. Not C with weird extensions that nobody will understand. Not C attempting to gussy up into a language with a real type system. Not C with a garbage collector. etc.

I don't get why so many people use C but don't want C. This isn't 1995 where you have to shoehorn everything into the C ecosystem because everything else sucked.

Re:Implementing Generic Types in C by spacedcowboy on Wednesday March 19, 2025 @04:28PM (#43416935)
But ... all of those things are still C. Maybe with extra libraries / steps / whatever, but still C.
Re:Implementing Generic Types in C by lukaslalinsky on Wednesday March 19, 2025 @05:32PM (#43417537)
See for example the last version, which is marked as "GOOD". It directly inlines all operations on the vector. The more you use it, the bigger the compiled code becomes. Need to debug it and want to set a breakpoint to vector_append? You can't. You also can't use callgrind/kcachegrind to see how fast the functions are. Yes, it's still C, but very awkward and finicky C.
Re:Implementing Generic Types in C by spacedcowboy on Wednesday March 19, 2025 @06:00PM (#43417737)
My point was that this is still C, I wasn't advocating using any of it.

People have different preferences though, so if someone has 50,000 lines of code invested in a C application, but wants to (for some reason) use generics in some way, perhaps this is their best option...

Re:Implementing Generic Types in C by churchturing on Wednesday March 19, 2025 @08:42PM (#43418818)
Any compiler worth its salt be inlining those functions (provided lto is enabled) because inlining that type of tiny function is a win. Additionally, the linker can deduplicate identical functions (eg vec append for two identically sized types)
Re:Implementing Generic Types in C by bsder on Wednesday March 19, 2025 @04:44PM (#43417121)
No ... this isn't C.

Will it have IDE support for the abstraction? Will a syntax mistake explode with a non-sensical error (yes, in this instance, because you are splicing macros and a syntax mistake will splice weird)? etc.

If you want C with Generic Types, use C++. Rather than splicing unknown garbage onto C, use a subset of your C++ features and you will get full IDE support, proper template errors (okay, maybe not an improvement), proper elision and inlining, etc.

Re:Implementing Generic Types in C by spacedcowboy on Wednesday March 19, 2025 @05:56PM (#43417711)
> No ... this isn't C.

My test for whether something or not is "still C" is very simple... "Does it compile with a C compiler"

These compile. They're C. End of debate.

Whether something has IDE support is neither here nor there. When I was learning C, your IDE was "vi" and "csh"... It was still C.

As for errors, completely normal C code can spew out errors up the wazoo if you miss an errant ;, " or } (to pluck examples out of the air) somewhere important, things like "function not defined" because you were using a function later on in the file and it can no longer be parsed. Error quality does not define the language either.

Re:Implementing Generic Types in C by usrnm on Friday March 21, 2025 @08:14AM (#43434710)
All C compilers can do inline asm, is asm C too?
Re:Implementing Generic Types in C by spacedcowboy on Friday March 21, 2025 @04:26PM (#43440429)
Jeez, there's always one..

Fine. My test for whether something consisting of C keywords, variable names, and symbols expected within the language as per the definition by some publicly agreed standard such as "C17", "C99" or "K&R", abhorring any non-standard extensions, other languages, or practices outside of the provided pre-processor, assembler and linker, and excluding any non-standard keywords that may or may not result in correct output, as pertaining to underlying architectural differences, is "still C" is very simple... "Does it compile with said C compiler"

Re:Implementing Generic Types in C by randomNumber7 on Wednesday March 19, 2025 @02:53PM (#43415950)
You could use C++ and ignore the rest of it.
Re:Implementing Generic Types in C by huhtenberg on Wednesday March 19, 2025 @03:21PM (#43416252)
Not sure why the parent is in gray. It's a perfectly valid and widely practiced approach.
Re:Implementing Generic Types in C by codr7 on Wednesday March 19, 2025 @04:36PM (#43417018)
How often it works out as intended is very much up for debate though.
Re:Implementing Generic Types in C by jimbob45 on Wednesday March 19, 2025 @05:09PM (#43417355)
For legacy codebases, switching the compiler is certainly out of the question. For everyone else, why do you think it would be an issue to use the C++ compiler?
Re:Implementing Generic Types in C by codr7 on Wednesday March 19, 2025 @05:55PM (#43417700)
Because the complexity seeps in through the cracks, and with the language comes a mindset; same reasons Linus is wary about C++ in the kernel.

Some say it works; I've never succeeded, even on solo projects.

Re:Implementing Generic Types in C by krapp on Wednesday March 19, 2025 @09:07PM (#43418961)
I think this is the general style for the Godot framework. They use a limited subset of C++, avoiding STL and some modern features, and limited templates.
Re:Implementing Generic Types in C by bfrog on Wednesday March 19, 2025 @08:46PM (#43418838)
I tried it once, ran out of memory debugging a template error as it blew up my terminal into a sea of jibberish.
Re:Implementing Generic Types in C by unclad5968 on Wednesday March 19, 2025 @03:58PM (#43416601)
C3 and Odin are some other alternatives.
Re:Implementing Generic Types in C by jdougan on Wednesday March 19, 2025 @08:55PM (#43418898)
Along with D
Re:Implementing Generic Types in C by lukaslalinsky on Thursday March 20, 2025 @05:49AM (#43421257)
D sits in a weird place and that's probably why it's not widely adopted.

If I'm using C, it's probably because I'm doing something low level and garbage collector could be problematic there. If you are fine with complexity, C++ or Rust are better options. If you want a simple language, I'd suggest Zig.

On the other hand, if I don't want to deal with memory management, and still want fairly fast compiled language, I'd just go with Go. It has a much better ecosystem, because low level GC language is really mostly targeted at server components and Go excels there with implicit async everywhere.

Re:Implementing Generic Types in C by pebal on Thursday March 20, 2025 @05:58AM (#43421302)
The creators of the D made a big mistake. Instead of focusing on improving the GC, they focused on making it possible to avoid GC in D. The Go has shown that this strategy was wrong.
Re:Implementing Generic Types in C by sirwhinesalot on Thursday March 20, 2025 @03:52AM (#43420641)
Zig is nice. Comptime is a really cool feature.