7.3. Invoking external functions via a pointer

A foreign import declaration imports an external function into Haskell. (The name of the external function is statically known, but the loading/linking of it may very well be delayed until run-time.) A foreign import declaration is then (approximately) just a type cast of an external function with a statically known name.

An extension of foreign import is the support for dynamic type casts of external names/addresses:

   : ...
   | 'foreign' 'import' [callconv] 'dynamic' ['unsafe']
            varid :: Addr -> (prim_args -> IO prim_result)

i.e., identical to a foreign import declaration, but for the specification of dynamic instead of the name of an external function. The presence of dynamic indicates that when an application of varid is evaluated, the function pointed to by its first argument will be invoked, passing it the rest of varid's arguments.

What are the uses of this? Native invocation of COM methods, [1] Haskell libraries that want to be dressed up as C libs (and hence may have to support C callbacks), Haskell code that need to dynamically load and execute code.



Or the interfacing to any other software component technologies.