Irritating shortcoming with modules and externs
Mike Parker
aldacron71 at yahoo.com
Tue May 16 22:44:17 PDT 2006
Jarrett Billingsley wrote:
>
>> How about moving to a delegate model instead of a direct global symbol
>> model? It's much more obvious, imho.
>
> That's probably what I'll end up doing, although the function that I wanted
> to do this with was main (so basically I could "intercept" the call to main
> in my library, and call the user-defined "LibMain" or something to that
> effect). Of course, seeing that D is an OOP language, I might as well make
> some kind of context class that wraps the entire program, ending you up with
> something like:
>
> abstract class Context
> {
> ...
> }
>
> class MyContext : Context
> {
> // override all those stubbed-out methods
> }
>
> void main(char[][] args)
> {
> MyContext c = new MyContext();
> c.main(args);
> }
>
>
How about this:
##########################################
// main.d
module main;
abstract class Context
{
public:
abstract void run(char[][] args);
protected:
this()
{
theContext = this;
}
}
private Context theContext;
void main(char[][] args)
{
theContext.run(args);
}
===========================================
// mygame.d
module mygame;
import main;
class MyContext : Context
{
public:
override void run(char[][] args)
{
// blah
}
}
static this()
{
new MyContext();
}
#######################################
I did something similar with a GameTask system I was working on. Works
like a charm.
More information about the Digitalmars-d-learn
mailing list