On Friday, 21 August 2015 at 21:37:34 UTC, Walter Bright wrote:
On 8/21/2015 4:44 AM, Marc =?UTF-8?B?U2Now7x0eiI=?= <[email protected]> wrote:
Just change Object.factory to require registration of the class.

What mechanism do you propose for that?

E.g.:

    template factoryConstructors(Args...) {
        // using void* because I don't know whether it's possible
        // to have a function pointer to a constructor
        void*[string] factoryConstructors;
    }

    void registerFactoryConstructor(Class, Args...)()
    if(is(Class == class))
    {
        factoryConstructors!Args[Class.stringof] = ...;
    }

    Object factory(Args...)(string className, Args args) {
        auto constructor = factoryConstructors!Args[className];
        return ...;
    }

This even allows to call constructors with arguments. deadalnix's proposal is a nice way to automate this for an entire class hierarchy. Another possible mechanisms would be some UDA magic.

Reply via email to