I've been thinking about C programming a lot lately, and what its limits are. This lead my to a very interesting C library call cello. I highly recommend reading this page, and looking through the source code. The author deserves some notice for such an interesting library.

Cello is described as a library that brings high level programming to C. This includes quite a lot of functionality such as generic data structures with mapping and folding, polymorphic functions, threading and synchronization, garbage collection, a type class / interface mechanism, and higher order functions. Its surprising how much leverage you can get from this- you can extend this library with new interfaces, functions, and structures, and get a good bit of functional programming into C as well as build high level interfaces. You can fit interator, streams, references, and all sorts of other things that are usually much more manual in C- its quite impressive to see an encoding of these concepts in C that is not an intrusive set of complex macros!

The way all of this is implemented seems to be with fat pointers, where a pointer points to data as usual, but there is extra metadata just before the pointer in memory. This technique is also used in the Forth community, where you can jump right to the code for a word, but can also back up and find things like the words name as a string and flags for its execution.

This mechanism allows pointers (all of these seem to be of type void&ask;) to have a structure of properties. It appears (through some quick check in the source code) that Cello looks up properties in some kind of table in order to dispatch the correct implementation for a function. This is more flexible than a v-table style, where these must be known statically for each class, but does incure some run-time overhead. Interestingly this makes it a bit more typeclassy in the Haskell sense (ignoring the dynamic types for now) because you have a single implemenation of an interface per type.

Unfortunately, while this is very interesting, I would not be able to use it for my work, and I would likely move to another language rather then accept a non-standard style in C. I'm used to a very restricted C style for high assurance systems, and this is too much of a pervasive change to how you use to language for me to invest time in using it. Howver, it is a certain point in the generic C design landscape with a novel set of tradeoffs compared to what I've seen done in C.