Hacker News new | ask | show | jobs
by preg_match 10 days ago
This is very unreliable overall and since the type system is so primitive, you’re limited on what you can enforce on the structure. You can easily break stuff accidentally.

For example: suppose you need a container to hold ints. But it also needs to be able to hold other types. In C++ this is trivial, as in virtually all modern languages. In C this is impossible.

You can have a generic memory backed storage + accessors. But if you rely on void pointer functions, you have to remember every time to cast and pinky promise to only store one type. It’s very, very easy to make a mistake, and you will.

Most abstractions that we take for granted in modern languages are straight up not possible in C, or at least they are extremely incomplete. Part of that is because you can’t get C to do anything automatically. Every function call must be manual.

1 comments

You are switching topics to polymorphic types. But you can also implement them in C in a type safe way without void pointers.

Here is an example for a generic vector type: https://godbolt.org/z/7MdGqcdbq

Your mistake is believing that you need native syntax for such feature to be possible. But this is not true.

Polymorphic types are pretty basic as far as type systems go, and this isn't even polymorphism in the traditional sense. In C++ these are concrete parameterized types, types of types, via templates.

And yes I'm aware of intrusive objects using macros, I've done this myself before. And you can even do evil stuff, if you want, like make `[]` work with them. But it's not reliable and it most definitely is a hack. Macros are like templates but if you strip out all of the type awareness.

We could probably go back and forth all day, so I'll just leave it here. I don't think you can reliably make abstractions in C, even pretty primitive ones, due to it's weakly typed nature combined with a primitive type system. Other languages don't have both. Go has historically had a primitive type system, but it was at least fairly strongly typed. Most modern languages are both strongly typed and have an in-depth type system.

It's not that these things are 100% impossible in C. It's that the language will fight you, will make it as difficult as possible, and the compiler will not check anything. In C, it's pinky promises all the way down. That works on a small scale but falls apart very quickly IMO. It's not about syntax or niceties, it's about the type system and what it does, and does not, guarantee. C guarantees basically nothing so you can't trust anything, ever.

This isn't just annoying and bug prone, it's also not very performant. If you've ever programmed in PHP then you know the annoyance of constantly having to verify everything. It's expensive. That's why std::sort is fast, and C's qsort is dog slow. The lack of a real type system makes many simple problems very difficult to solve in a performant way.

Before you said "impossible", now you merely claim the language "fights" you. But I do not see how the language fights me, it is trivial and convenient for me to write abstractions in C, including polymorphic types. In fact, I find it far more enjoyable to write C than C++.

Those types can be written in a way that C's type system checks type safety, and my example was of this kind. No "pinky promises" necessary. In fact, these types are very similar to templates in C++. I would even agree that there are bad, but just in the same way templates in C++ are also bad.

And finally: you can also write a sort that inlines its comparator in C as well.

I mean I just disagree fundamentally. It’s not trivial, it’s not convenient. It takes much, much more code and barely works when it does. And to top it off, it’s trivial to break, which is why we see it broken in just about every C codebase on the planet of any sort of large size.

That’s not to say C++ is perfect, it’s clearly not. It’s just that C is worse. The way I see it, there’s really no reason to ever write application code in C. It will take more code, it will be more buggy, and it will most likely be much slower. The reality is that garbage collected languages do often end up being faster because most allocation techniques in C and C++ are naive and expensive.

Which is why we see almost zero applications written in C. They’re written in Java, dotnet, Go, and others. Only low-level libraries are written in C these days because writing C sucks ass.

If you decided that this is what you want to believe, I guess no argument will convince you. But note that I was programming in C++ before switching (almost) fully to C, and I am telling you that I prefer writing C - by a lot!
Take a look at raddebugger, it's written in C.

It compiles instantly (a full debug rebuild), starts instantly, have really nice GUI and are insanely fast and responsive. The codebase is very understandable and easy to hack on, and has as set of great abstractions that make all the supposedly annoying things in C (strings, memory allocation, multi threading) quite nice to use.

If you insist on using the C standard library and malloc()ing all over the place, using null terminated strings, complicated lifetime hierarchies and infinitely "generic code" (where you end up using max 1 or 2 different implementations/types in the end), then yes, you will have a bad time in C, but how about just not doing that?