Functions for generating private code in D?

I am writing a template structure in D that uses a string mixin and compiles time functions for some of its functions. Basically, it is in this format:

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

Looking at this, genCode() clearly part of the implementation of my template class; By making it public, it reveals a logic that must be truly private and that can be changed. It also clutters the export space exported by the module.

When I try to make it private , D throws an error. As far as I can tell, the expression in the mixin string is evaluated in any area of MyTemplate , as a result of which D claims that the genCode() character is not declared.

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

+6
source share
1 answer

Please provide code examples that demonstrate the topic.

 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; ma = 3; } 

The required access level must be selected: public, private, package. These are the only controls that provide access levels. If something else is not required, this is not possible.

Related errors:

+1
source

Source: https://habr.com/ru/post/969823/


All Articles