|
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. |
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.