www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Object.factory

How reliable is Object.factory?  That is, is there any case where the 
compiled program would lose this functionality (some specific 
incantation of options to avoid giving the compiler), and is it an 
actual part of the language specification for Object or a dmd-specific 
"dangerous" feature?  I'd much like to know, because I just learned to 
love it.

This:
//////////////////////////////////////////////////
public static Command Instance () {
     static Command
         instance = null ;

     if ( instance is null ) {
         instance = cast( Command ) factory(
             join( [ "app.commands.", ".", "Command" ], capitalize( 
Config.command ) )
         );
     }
     return instance;
}
//////////////////////////////////////////////////

Is so much better than a switch statement or a registry for deferred 
singletons and the like, despite the (shudder) explicit cast involved.

-- Chris Nicholson-Sauls
Sep 29 2009