Not able to load classes defined in archive file in Mac OSX using Object.factory

Adam D. Ruppe destructionator at gmail.com
Fri Nov 16 19:36:54 UTC 2018


On Friday, 16 November 2018 at 14:55:52 UTC, Aditya wrote:
> How can one write own factory function ? Any pointers to this ?

Something like

Interface delegate()[string] factories;

Interface create(string className) {
     if(className in factories)
        return factories[classname];
     else
        return null; // or throw if you prefer
}



In every module in which you create a class, you will want to add 
code like this:

class Class : Interface {}

// register with the factory in a module constructor
static this() {
    factories["Class"] = Interface delegate() {
        return new Class();
    }
}


And it should work.


More information about the Digitalmars-d-learn mailing list