# A modular foreign function interface

17 Jul 2017

Foreign function interfaces are typically organised monolithically, tying together the $\textit{specification}$ of each foreign function with the $\textit{mechanism }$ used to make the function available in the host language. This leads to inflexible systems, where switching from one binding mechanism to another (say from dynamic binding to static code generation) often requires changing tools and rewriting large portions of code. We show that ML-style module systems support exactly the kind of abstraction needed to separate these two aspects of a foreign function binding, leading to declarative foreign function bindings that support switching between a wide variety of binding mechanisms — static and dynamic, synchronous and asynchronous, etc. — with no changes to the function specifications. Note. This is a revised and expanded version of an earlier paper, $\textit{Declarative Foreign Function Binding Through Generic Programming.}$ This paper brings a greater focus on modularity, and adds new sections on error handling, and on the practicality of the approach we describe.