Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Private code generation functions in D?

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?

like image 738
exists-forall Avatar asked May 25 '14 04:05

exists-forall


People also ask

What is the main purpose of code generator?

Code generator converts the intermediate representation of source code into a form that can be readily executed by the machine. A code generator is expected to generate the correct code. Designing of the code generator should be done in such a way so that it can be easily implemented, tested, and maintained.

What is foo function in MATLAB?

In this context, foo is referred to as an extrinsic function. This functionality is available only when the MATLAB engine is available during execution. Examples of such situations include execution of MEX functions, Simulink® simulations, or function calls at the time of code generation (also known as compile time).

What are code generation techniques?

In computing, Code generation denotes software techniques or systems that generate program code which may then be used independently of the generator system in a runtime environment.

What is code generation and example?

The code generated by the compiler is an object code of some lower-level programming language, for example, assembly language.


1 Answers

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:

  • https://issues.dlang.org/show_bug.cgi?id=3108
  • https://issues.dlang.org/show_bug.cgi?id=8716
  • https://issues.dlang.org/show_bug.cgi?id=7961
like image 112
he_the_great Avatar answered Oct 13 '22 20:10

he_the_great