How do I make an extern function?

BCS none at anon.com
Tue Jun 29 07:03:39 PDT 2010


Hello Simen,

> BCS <none at anon.com> wrote:
> 

>> You can resolve this by having a a.di file with the extern foo(); in
>> it  (DMD has a flag to generate such a file for you). OTOH without
>> knowing  what you are doing, I can't tell if this is the correct
>> solution.
>> 
> I'm trying to create a framework in which the user may provide his own
> foo( ), so the name of module b is impossible to know.
> 

Several approaches: In addition to interfaces as Steven pointed out, if you 
don't need overloading you can use a function pointer. Also for either option, 
you could have a global variable that the users code sets from a static this:

module a;

void function(int) foo;

// use foo

------
moduel b;
import a;

void theFoo(int i) { ... }
static this() { foo = &theFoo; }

If you want more encapsulation, you could switch to a registration function 
rather than having people muck around in the dirt. Also, if you have some 
appropriate object, you can put it there and avoid a global and all it entails.

-- 
... <IXOYE><





More information about the Digitalmars-d-learn mailing list