6

I'm writing a templated struct in D, which uses string mixins and compile time functions for some of its functionality. Basically, it is in this format:

string genCode(T)() {
    // ...
}

struct MyTemplate(T) {
    mixin(genCode!(T)());
    // ...
}

Looking at this, genCode() is clearly an implementation detail of my template class; making it public exposes logic which should really be private, and which could be subject to change. It also clutters the module's exported namespace.

When I try to make it private, however, D throws an error. As far as I can tell, the expression in the string mixin is evaluated in whatever scope MyTemplate is instantiated in, which caused D to claim that the symbol genCode() is not declared.

Is there any way around this? Do I just have to live with genCode() as a public function, or is there some way I can hide it?

exists-forall
  • 4,148
  • 4
  • 22
  • 29

1 Answers1

1

Please provide code examples which demonstrate the subject.

module x.gen;
private string genCode(T)() {
    return T.stringof ~ " a;";
}

module x.test;

import x.gen;
struct MyTemplate(T) {
    mixin(genCode!(T)());
}

void main() {
    MyTemplate!int m;
    m.a = 3;
}

The access level desired must be select: public, private, package. These are the only controls provided for access levels. If anything else is desired it isn't possible.

Related Bugs:

he_the_great
  • 6,554
  • 2
  • 30
  • 28