Hacker News new | ask | show | jobs
by naitgacem 909 days ago
Excellent writeup and straight to the point. As the author demonstrated one can get quite a lot of OOP constructs using C primitives.

what seems to be impossible to implement (as least to me) was something like interfaces, a way to decouple in a way such that high level functions don't need to know about the low level building blocks.

6 comments

Um, this is how software was built in the olden days. C++ literally began as "we could write a preprocessor to automate the tricks everyone uses to implement polymorphism in C" (CFront). And prior to that, the same tricks were used in assembly language programming. So this article has recreated the history of OOP, which was to create tooling to better support programming techniques already widely used. It wasn't some religion invented by the priests and sent forth on tablets, although due to humans loving them some cult, it became that eventually.
We used to have books about it, and stuff like COM, SOM, CORBA also support C, which is exactly what you're referring to.

Regarding books, here is one from 1993, https://www.mclibre.org/descargar/docs/libros/ooc-ats.pdf

Links to Axel-Tobias Schreiner's books:

https://www.cs.rit.edu/~ats/books/

"Object-Oriented Programming with ANSI-C" (English):

https://www.cs.rit.edu/~ats/books/ooc.pdf

"Objekt-orientierte Programmierung mit ANSI-C" (German):

https://www.cs.rit.edu/~ats/books/ooc_de.pdf

Classic..!

I wonder how it feels now, 20 years after reading it for the first time. I remember how revelatory it felt.

Yes, You can. The entire Linux device interface, to name just one example, if full of interfaces. The way to accomplish this is via pointer to functions, and to have it as an object, have these pointer to functions grouped in a struct. GTK/Glib is notably full of these interfaces too.
Yup. In a past job I did a lot of work writing ALSA drivers for custom souncards. The ALSA interface is a good example of this. They provide an API app developers can use to do sound stuff (change the volume, for example). In your sound card driver you provide an implementation of the API to do whatever changing the volume means for your particular hardware (in my case sending an i2c message to a digital potentiometer).

https://www.kernel.org/doc/html/v4.14/sound/kernel-api/alsa-...

libusb is a good reference for this as well, and well documented.
One thing to look at is ffmpeg in its encoders and decoders. At the bottom of the file there's a struct with pointers to functions (among other things). Anything that wants to do decoding can just call init(), decode(), close() on an AVCodec and the internal functions do whatever they need to do. Here's one from h264.c:

    AVCodec ff_h264_decoder = {
      .name     = "h264",
      .type     = AVMEDIA_TYPE_VIDEO,
      .init     = ff_h264_decode_init,
      .close    = h264_decode_end,
      .decode   = h264_decode_frame,
      ... more fields ...
    };
When you read or write from a FILE in C, do you know or have to care about whether the FILE comes from disk, a pipe, a CD drive, or a device driver or a network mounted drive? What do you think FILE is if not an interface?
You can approximate it by maintaining some kind of vtable, but it can get messy, especially if you need to implement multiple traits/interfaces.