static this [was: any tool to at least partially convert C++ to D]

Justin Johansson no at spam.com
Wed Mar 10 12:51:38 PST 2010


BCS Wrote:

> I just had a crazy thought: how about allow things to explicitly state static-constructor 
> dependencies? By default, it would work as it does now, all constructors 
> in all imported modules get run before any in this module but if an explicit 
> list is given, then only that list need be run first.
> 
> My first thought was to only do this at the module level with something like 
> this:
> 
> module foo.bar : foo.baz, foo.baz; // run first, even thought not imported
> 
> import foo.bar;
> import foo.bing; // constructors need not be run first
> 
> It might be handy to allow finer grained specification of both the ends, 
> maybe putting the list per constructor and allowing the list to include classes 
> for instance.

It's not a crazy idea at all, unless I am crazy also :-)

This is similar to the idea that I am now using in C++ to solve the same problem,
albeit a bit of a hack since there is no language support for it.  My idea (in C++)
is simply to have an abstract Module class which has a virtual method initialize() 
that concrete Module classes implement.  Then you statically instantiate a
single instance of each Module.  At runtime, these instances are constructed
in some linker dependent order (this being a manifestation of the original problem).
But then in the constructor for Module, you list the dependencies upon other Module
classes and use this information to create a direct acyclic graph (DAG) for ultimate
invocation of the initialize() methods of all your "modules" in dependency order
(upon entering main()).

Also you can use placement operator new to construct "static objects" out of static
memory (if need be), this being called in the appropriate implementation of the
initialize() method for the respective "module".

Works for me but YMMV.

Cheers
Justin Johansson






More information about the Digitalmars-d mailing list