Sanity checks

So, since i have (currently) 5 working and 1 WIP DLL for the backup system, and since four of these share the exact same interface,, i thought it would be a proverbial Good Idea (tm) to force compliance - force the signature of all exported functions to conform to a predefined type.

Let me explain. I can have a function called MyFunc, it looks like this: function MyFunc(inP: Integer) : String;

so it takes an integer and returns a string. Why not.

What I wanted to do was define a type : type TMyFunc = function(inP: Integer) : String;

then define the real functions in each DLL as conforming to the type TMyFunc. That way if I change the definition of TMyFunc, the compiler will let me know because the functions are no longer defined correctly.

Well, to cut a long story short, you can't. You cannot define a function according to a type. Which is a bit of a pain really. Wouldn't be useful to very many people I suppose, but in my case, it would have been great.

So to work around this, I did it another way. In the initialization section of the DLL, I now have a little but of code:

var lMyFunc : TMyFunc;
begin
{$HINTS OFF}
  lMyFunc := MyFunc;
end;

(I turn off the hints so that the compiler won't tell me i have a variable to which I assign a value and then never use it).

So I have a variable of type TMyFunc - a variable which is a function. And I assign a real function (MyFunc) to that variable.

Works perfectly, and if I change the definition of TMyFunc (which is in a shared unit), the compiler will complain that "parameter lists differ". I have to do this manually for all exported functions from my DLL, but it is better than nothing!

Now to continue implementing the stuff. Doing this added quite a few items to my TODO list...and doing this wasn't even ON the TODO list! But having a caller fail because the DLL was compiled with an old version of the interface is not really "cool". I prefer to have the compiler tell me, rather than users...