Hourglass interfaces

A friendly redditor put me on the path of the Hourglass Interface. It’s a pattern that facilitates stable ABI interfaces, such as those between program and library. If you control all the source code this is usually not an issue, but even if you do, you may want to be able to use libraries and programs compiled with various compilers. In particular, this is a problem with C++, where something as basic, yet complex, as std::string is not binary compatible between compilers, and not even between compiler versions (you know who you are, Visual Studio. At least Microsoft seems to have changed this with VS2017).

So, what’s a stable ABI interface? C! Our old, reluctantly typed friend is the defacto binary interface because it’s simple, and never really changes. Well, it has, but C89 is spoken by virtually every FFI (foreign function interface) out there. So, Stefanus Du Toit argues, it is the perfect solution for solving ABI compatibility. It even facilitates creating binding with e.g. SWIG, because you’ll be designing the interface intentionally, rather than letting SWIG figure it out (probably suboptimally, which it can’t really help). The hourglass refers to the interface: we must squeeze, from out fat application, through C89, to the again (possible) fat library. Of course, you are free to chose something else, as long as your language can cooperate. Stefanus placed his example here.

Another comment on reddit pointed to cppcomponents, a library that automates some of this. I haven’t looked into it very deeply yet, but for sure I’m keen to try out this pattern.