Parameterized types in C using the new tag compatibility rule

ingve | 107 points

The recent #def #enddef proposal[1] would eliminate the need for backslashes to define readable macros, making this pattern much more pleasant, finger crossed for its inclusion in C2Y!

[1] https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3531.txt

fuhsnn | 9 hours ago

Neat similarity to Zig's approach to generic types. The generic type is defined as a type constructor, a function that returns a type. Every instantiation of that generic type is an invocation of that function. So the generic growable list type is `fn ArrayList(comptype T: type) type` and a function that takes two lists of i32 and returns a third is `fn foo(a: ArrayList(i32), b: ArrayList(i32)) ArrayList(i32)`

Arnavion | 2 hours ago

Not personally interested in this hack, but https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3037.pdf means struct foo {} defined multiple times with the same fields in the same TU now refers to the same thing instead of to UB and that is a good bugfix.

JonChesterfield | 6 hours ago

If you're reaching for that hack, just use C++? You don't have to go all in on C++-isms, you can always write C-style C++ and only use the features you need.

IAmLiterallyAB | 4 hours ago

I think this is an interesting change, even though I (as someone who has loved C for 30+ years and use it daily in a professional capacity) don't immediately see a lot of use-cases I'm sure they can be found as the author demonstrates. Cool, and a good post!

unwind | 10 hours ago

Here is my experimental library for generic types with some godbolt links to try: https://github.com/uecker/noplate

uecker | 3 hours ago

Slighty off-topic, why is he using ptrdiff_t (instead of size_t) for the cap & len types?

rwmj | 9 hours ago
[deleted]
| 2 hours ago

Are we getting a non-broken `_Generic` yet? Because that's the thing that made me give up with disgust the last project I tried to write in C. Manually having to do `extern template` a few times is nothing in comparison.

o11c | 3 hours ago

i fear this will make slopy code compile more often OK.

Surac | 9 hours ago

It seems as though this makes it impossible to do the new-type paradigm in C23 ? If Goose and Beaver differ only in their name, C now thinks they're the same type so too bad we can tell a Beaver to fly even though we deliberately required a Goose ?

tialaramex | 9 hours ago