155 points•AlexeyBrin•about 2 months ago•137 comments•

137 comments

lor_louisabout 2 months ago
I wrote a summarizing article on type safe container types a while back, but with some C23 specific changes and a few tweaks to work better for complex types.

https://louissven.xyz/article/how_I_do_container_types_in_C....

Feel free to flag/delete if this isn't the place.

rramadassabout 2 months ago
Thanks for sharing.

It is really nice that you studied both Martin Uecker and Daniel Hooper's techniques and tweaked them to suit your ideas/taste.

Panzerschrekabout 2 months ago
I don't understand people reinventing macro-based hacks in C to achieve what was achieved in other languages many years ago. Why not using C++, for example? It's available almost everywhere, introducing its usage in an existing C codebase is pretty simple. Sure, C++ has its own downsides, but is it better to create mess with macros in C rather then using exiting language facilities and standard library containers provided by C++?
ueckerabout 2 months ago
Having used C++ a lot in the past, I think the mess in C++ is way worse. I also contribute to projects that introduce C++ into a C code base, and I really wish they hadn't done this.

Although there are C features I like that I would need to remove before introducing C++ to a codebase, so this may not always be so simple.

Panzerschrekabout 2 months ago
> the mess in C++ is way worse

What is mess in C++? Yes, it has some shady parts and is more complex than C, but this complexity provides expressiveness and type safety. And you can always use only features from C++ you find useful.

> also contribute to projects that introduce C++ into a C code base, and I really wish they hadn't done this.

What is problematic in these projects other than unfamiliarity of C++ for developers previously used only C?

uhuauaabout 2 months ago
If C++ adds pattern matching, and it works in practical usage across projects, will the C ecosystem consider adding some kind of pattern matching, perhaps inspired by what comes into C++, perhaps not?

Also, a question for a C expert like you, uecker: In your opinion, does the compound literals feature have complex lifetimes rules or complex semantic rules otherwise?

Joker_vDabout 2 months ago
For one reason, it is because C++ runtime (and its standard library) is a whole own can of worms which most people would rather not touch if they can afford to. Which they mostly can.

> Sure, C++ has its own downsides

"Sure, getting your eyes gouged out has its downsides, but is it better to read that awful mess of macros in C instead?" The answer most people would give to this question may surprise you.

kccqzyabout 2 months ago
Here we go again. Yes there are bad parts in the C++ standard library and there are good parts. But if you are just trying to do type safe generic data structures you are unlikely to touch the bad parts. Many codebases forbids parts of the standard library, e.g. LLVM forbids including <iostream>. You can forbid using parts of the standard library too.
psyclobeabout 2 months ago
Its rather hard to introduce c++ into legacy c projects. You basically have to decide on a subset of features to use, and then you'll have to explain to the teams why the same looking code now takes 10x more compute to build.

Usually the way we do it here is we honor some interface then rewrite the subsystem in c++.

And then there's the real hurdle and that is getting the c++ idiom correct as it is very easy to just open the floodgates and let everyone write code that looks vastly different.

rwbtabout 2 months ago
I'd rather not migrate my C codebase to C++ just to use an array container. Very hard to consistently limit the codebase to a strict subset of C++.
pjmlpabout 2 months ago
It is called a linter, more devs should learn to use a tool that was originally created for C in 1979.
0xbadcafebeeabout 2 months ago
If you want better tires on your car, why not just get a different car?
rramadassabout 2 months ago
Also see Templates in C by David Priver - https://www.davidpriver.com/ctemplates.html

He also has other interesting C techniques, namely;

Adding reflection to C - https://news.ycombinator.com/item?id=49964525

_Generic for Type Reification in C - https://www.davidpriver.com/creification.html

See also his C2y interpreter with REPL named "DrC" for the upcoming C29 standard (https://en.wikipedia.org/wiki/C29_(C_standard_revision)) - https://github.com/drpriver/drc

PS: I really like his style of writing and presentation; concise and precise without unnecessary fluff and page beautifying.

david2ndaccountabout 2 months ago
Hey that’s me! Cool to see people like my stuff.
rramadassabout 2 months ago
You should write more ;-)

I also posted your "Adding Reflection to C" at https://news.ycombinator.com/item?id=49964525 and hope HN picks up on it and has a decent discussion.

You definitely should add a detailed post on your "DrC" compiler/interpreter; usecase, scope, techniques used etc. Since this is looking forward to C2y/C29 with some more extensions, i think people will enjoy playing with it in the REPL format.

warmwafflesabout 2 months ago
I do wish to see comptime stuff in C some day. Or at a minimum your `defblock` implemented. I am so tired of having to escape new lines.
podocarpabout 2 months ago
Lol at some point just implement a small compiler in C… reflection is kinda a hilarious thing to have like I understand the use case for generics, since it's not that that that hard to do with macros and quite commonly wished for
tniemiabout 2 months ago
That `(1 ? (item) : (list)->payload)` was a neat trick. It gets optimized away, but the type comparison happens before that.

Still feels a bit like a party trick, but if it works...

JdeBPabout 2 months ago
It's somewhat of a forced trick nowadays, I think.

I actually thought from the title before I read the article that it was going to use _Generic, as in something like _Generic((item),__typeof__((list)->payload):...) .

JonChesterfieldabout 2 months ago
You want attribute((overloadable)). Not _Generic. Noone wants _Generic, it should not have been proposed, nor specified, nor implemented.
Xirdusabout 2 months ago
Expressions that compile but never evaluate are bread and butter in C++ template metaprogramming.
veexx103about 2 months ago
Is it feasible to create a new language by adding features to C

just as was done with C++?

pornelabout 2 months ago
It's not worth it. The value of C is in compatibility, availability of compilers, and familiarity.

As soon as you change anything in a breaking way, start requiring your own compiler/transpiler, or introduce new idioms, you end up losing the things that keep C alive.

Users of C either like it exactly the way it is, or have to use a specific C version due to a vendor dependency or compliance.

An upgraded C not approved by the standards body gives you yet another niche language that is not C, but is still burdened with its old flaws.

rramadassabout 2 months ago
Well said.

I really don't understand why people bring up other languages when one is discussing implementation of some "advanced/tricky/hackish/new" features in C. It is not as if the implementer does not know about the ease of availability in other languages but there is always some set of criteria which prevents switching to a new language.

On a related note, there is a dearth of written/learning material (books etc.) cataloging and explaining advanced architecture/design/implementation patterns in C though we know they exist in the tons of industrial-strength codebases out there.

There is also the fact that when you see an implementation in C of some feature from another language you better understand language design pragmatics eg. implementing inheritance and virtual functions in C gives you insight into how they work in C++/Java/C#/etc.

actionfromafarabout 2 months ago
It would be nice to have something like C which was very much like C and Pascal (not the syntax) but which was basically C except no naked pointers by default etc, fewer footguns, but which would generate C.
pjmlpabout 2 months ago
And Objective-C, yet somehow people reinvent them badly in C.
accelbredabout 2 months ago
I am working on a language that was C with extensions, transpiled to plain C, but the C syntax is kinda not great to work with (stuff like needing type tables and unbounded lookahead). At the point you clean up the syntax, you're not really C with extensions anymore.
FullGarden_Sabout 2 months ago
C3 might be it because C3 has full C ABI compatibility so unlike most modern C-like alternatives, it checks out.
ActorNightlyabout 2 months ago
I mean you dont need to make a "language" at all, you can just write a librar for all the stuff you need. This stuff is just syntactic sugar.

For example, when I worked on writing embedded software for autopilots, we had 2 files, common.so and common.h that contained essentially a super robust typing framework. The way it worked is that you defined types through the provided functions at the beginning of your code, and all of the definitions were stored and checked at runtime at the beginning before the main execution loop took effect. So basically taking what a more robust language compiler would do and just moving that processing to the start of execution.

I use something similar for my local llm setup at home, where i can have my agent basically write tools for itself and when the tool fails, the error code makes it pretty easy for the agent to fix.

stianhoilandabout 2 months ago
Congrats, you (re-)invented Objective-C!

Read the full thread on Hacker News →

Related stories