Object.factory() and module name

eles eles at eles.com
Fri Apr 26 08:34:00 PDT 2013


Creating a class instance from a string with Object.factory() 
method requires the name of the module:

http://www.informit.com/articles/article.aspx?p=1381876&seqNum=6

module main;
Human person = cast(Human)Object.factory("main.Human");

However, if one rename the module (let's say into "newmain"), he 
also has to replace all the occurences of "main.Human" into 
"newmain.Human" inside the same source file.

The situation is somewhat worse if the default module name is 
kept, that is the name of the source file. Renaming the source 
file will instantly break use of the .factory() method and, if 
the programmer is not careful to test for !is null after each 
.factory(), things get easily corrupted.

For these reasons, classes that belongs to the current module 
should (well, it's a proposal) allow a faster creation method.

Proposals:

Object.factory("Human"); // idem as Object.factory("<module 
name>.Human");

or

Object.factory(".Human"); // idem as Object.factory("<module 
name>.Human");


or

Object.factory("module.Human"); // idem as 
Object.factory("<module name>.Human");

The third proposal is, however, a bit cumbersome since requires 
the compiler to reinterpret part of a string, but it takes 
advantage of the fact that one canot name a module "module".

This way, one could rename source files without worrying for this 
issue. More, maybe it is better for the Object.factory() method 
to throw an exception instead of simply returning a null pointer 
(this will be seen even if the programmer forgot to test for null 
and is an alternative solution to the above proposals).

Opinions?


More information about the Digitalmars-d mailing list