On Thursday, 31 March 2016 at 13:10:49 UTC, Steven Schveighoffer wrote:

I too like Voldemort types, but I actually found moving the types outside the functions quite straightforward. It's just annoying to have to repeat the template parameters. If you make them private, then you can simply avoid all the constraints. It's a bad leak of implementation, since now anything in the file has access to that type directly, but it's better than the issues with voldemort types.


If you move anything with a Voldemort type to their own modules, then do what you say, then there is no longer an access issue. Leads to a proliferation of modules.

I can think of another alternative, but it is probably a needless complexity. Suppose there is a protection attribute with the property that things in the module can only access it if given permission explicitly. For instance, taking the D wiki Voldemort type example and modifying it to your approach would give

struct TheUnnameable
{
        int value;
        this(int x) {value = x;}
        int getValue() { return value; }
}

auto createVoldemortType(int value)
{
    return TheUnnameable(value);
}

The Unnameable would then be changed to

explicit struct TheUnnameable
{
        explicit(createVoldemortType);
        int value;
        this(int x) {value = x;}
        int getValue() { return value; }
}

where explicit used as a protection attribute would restrict TheUnnameable to only things where the explicit function gives explicit permission.

Reply via email to