www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How give a module to a CTFE function

reply Xavier Bigand <flamaros.xavier gmail.com> writes:
Hi,

I have a CTFE function that I want to make more generic by given it a 
module as parameter.


My actual code looks like :

mixin(implementFunctionsOf());


string implementFunctionsOf()
{
     import std.traits;

     string	res;

     foreach(name; __traits(allMembers, myHardCodedModule))
     {
     }
     return res;
}


I tried many things but I can't figure out the type of the parameter I 
should use for the function implementFunctionsOf.
Mar 12 2018
parent reply arturg <var.spool.mail700 gmail.com> writes:
On Monday, 12 March 2018 at 21:00:07 UTC, Xavier Bigand wrote:
 Hi,

 I have a CTFE function that I want to make more generic by 
 given it a module as parameter.


 My actual code looks like :

 mixin(implementFunctionsOf());


 string implementFunctionsOf()
 {
     import std.traits;

     string	res;

     foreach(name; __traits(allMembers, myHardCodedModule))
     {
     }
     return res;
 }


 I tried many things but I can't figure out the type of the 
 parameter I should use for the function implementFunctionsOf.
you can use a alias or a string: void fun(alias mod, string mod2)() { foreach(m; __traits(allMembers, mod)) pragma(msg, m); foreach(m; __traits(allMembers, mixin(mod2))) pragma(msg, m); } void main() { import std.stdio; fun!(std.stdio, "std.stdio"); }
Mar 12 2018
parent reply Xavier Bigand <flamaros.xavier gmail.com> writes:
Le 12/03/2018 à 22:30, arturg a écrit :
 On Monday, 12 March 2018 at 21:00:07 UTC, Xavier Bigand wrote:
 Hi,

 I have a CTFE function that I want to make more generic by given it a
 module as parameter.


 My actual code looks like :

 mixin(implementFunctionsOf());


 string implementFunctionsOf()
 {
     import std.traits;

     string    res;

     foreach(name; __traits(allMembers, myHardCodedModule))
     {
     }
     return res;
 }


 I tried many things but I can't figure out the type of the parameter I
 should use for the function implementFunctionsOf.
you can use a alias or a string: void fun(alias mod, string mod2)() { foreach(m; __traits(allMembers, mod)) pragma(msg, m); foreach(m; __traits(allMembers, mixin(mod2))) pragma(msg, m); } void main() { import std.stdio; fun!(std.stdio, "std.stdio"); }
I tried both without success, Here is my full code : module api_entry; import std.stdio : writeln; import std.algorithm.searching; import derelict.opengl3.functions; import std.traits; string implementFunctionsOf(string mod) { import std.traits; string res; static foreach(name; __traits(allMembers, mixin(mod))) { static if (mixin("isCallable!" ~ name) && name.startsWith("da_")) { string oglFunctionName = name[3..$]; string returnType = ReturnType!(mixin(name)).stringof; string parametersType = Parameters!(mixin(name)).stringof; res ~= "export\n" ~ "extern (C)\n" ~ returnType ~ "\n" ~ oglFunctionName ~ parametersType ~ "\n" ~ "{\n" ~ " writeln(\"" ~ oglFunctionName ~ "\");\n"; static if (ReturnType!(mixin(name)).stringof != "void") { res ~= " " ~ returnType ~ " result;\n" ~ " return result;"; } res ~= "}\n"; } } return res; } mixin(implementFunctionsOf("derelict.opengl3.functions")); As string I get the following error: ..\src\api_entry.d(16): Error: variable `mod` cannot be read at compile time ..\src\api_entry.d(48): called from here: `implementFunctionsOf("derelict.opengl3.functions")` I also tried to make implementFunctionsOf a mixin template.
Mar 12 2018
next sibling parent reply Xavier Bigand <flamaros.xavier gmail.com> writes:
Le 12/03/2018 à 23:24, Xavier Bigand a écrit :
 Le 12/03/2018 à 22:30, arturg a écrit :
 On Monday, 12 March 2018 at 21:00:07 UTC, Xavier Bigand wrote:
 Hi,

 I have a CTFE function that I want to make more generic by given it a
 module as parameter.


 My actual code looks like :

 mixin(implementFunctionsOf());


 string implementFunctionsOf()
 {
     import std.traits;

     string    res;

     foreach(name; __traits(allMembers, myHardCodedModule))
     {
     }
     return res;
 }


 I tried many things but I can't figure out the type of the parameter I
 should use for the function implementFunctionsOf.
you can use a alias or a string: void fun(alias mod, string mod2)() { foreach(m; __traits(allMembers, mod)) pragma(msg, m); foreach(m; __traits(allMembers, mixin(mod2))) pragma(msg, m); } void main() { import std.stdio; fun!(std.stdio, "std.stdio"); }
I tried both without success, Here is my full code : module api_entry; import std.stdio : writeln; import std.algorithm.searching; import derelict.opengl3.functions; import std.traits; string implementFunctionsOf(string mod) { import std.traits; string res; static foreach(name; __traits(allMembers, mixin(mod))) { static if (mixin("isCallable!" ~ name) && name.startsWith("da_")) { string oglFunctionName = name[3..$]; string returnType = ReturnType!(mixin(name)).stringof; string parametersType = Parameters!(mixin(name)).stringof; res ~= "export\n" ~ "extern (C)\n" ~ returnType ~ "\n" ~ oglFunctionName ~ parametersType ~ "\n" ~ "{\n" ~ " writeln(\"" ~ oglFunctionName ~ "\");\n"; static if (ReturnType!(mixin(name)).stringof != "void") { res ~= " " ~ returnType ~ " result;\n" ~ " return result;"; } res ~= "}\n"; } } return res; } mixin(implementFunctionsOf("derelict.opengl3.functions")); As string I get the following error: ..\src\api_entry.d(16): Error: variable `mod` cannot be read at compile time ..\src\api_entry.d(48): called from here: `implementFunctionsOf("derelict.opengl3.functions")` I also tried to make implementFunctionsOf a mixin template.
I forgot to precise, that I don't have a main, because I am trying to create an opengl32.dll. This is why I already have a mixin to inject to function definitions in the root scope.
Mar 12 2018
next sibling parent reply Xavier Bigand <flamaros.xavier gmail.com> writes:
Le 12/03/2018 à 23:28, Xavier Bigand a écrit :
 Le 12/03/2018 à 23:24, Xavier Bigand a écrit :
 Le 12/03/2018 à 22:30, arturg a écrit :
 On Monday, 12 March 2018 at 21:00:07 UTC, Xavier Bigand wrote:
 Hi,

 I have a CTFE function that I want to make more generic by given it a
 module as parameter.


 My actual code looks like :

 mixin(implementFunctionsOf());


 string implementFunctionsOf()
 {
     import std.traits;

     string    res;

     foreach(name; __traits(allMembers, myHardCodedModule))
     {
     }
     return res;
 }


 I tried many things but I can't figure out the type of the parameter I
 should use for the function implementFunctionsOf.
you can use a alias or a string: void fun(alias mod, string mod2)() { foreach(m; __traits(allMembers, mod)) pragma(msg, m); foreach(m; __traits(allMembers, mixin(mod2))) pragma(msg, m); } void main() { import std.stdio; fun!(std.stdio, "std.stdio"); }
I tried both without success, Here is my full code : module api_entry; import std.stdio : writeln; import std.algorithm.searching; import derelict.opengl3.functions; import std.traits; string implementFunctionsOf(string mod) { import std.traits; string res; static foreach(name; __traits(allMembers, mixin(mod))) { static if (mixin("isCallable!" ~ name) && name.startsWith("da_")) { string oglFunctionName = name[3..$]; string returnType = ReturnType!(mixin(name)).stringof; string parametersType = Parameters!(mixin(name)).stringof; res ~= "export\n" ~ "extern (C)\n" ~ returnType ~ "\n" ~ oglFunctionName ~ parametersType ~ "\n" ~ "{\n" ~ " writeln(\"" ~ oglFunctionName ~ "\");\n"; static if (ReturnType!(mixin(name)).stringof != "void") { res ~= " " ~ returnType ~ " result;\n" ~ " return result;"; } res ~= "}\n"; } } return res; } mixin(implementFunctionsOf("derelict.opengl3.functions")); As string I get the following error: ..\src\api_entry.d(16): Error: variable `mod` cannot be read at compile time ..\src\api_entry.d(48): called from here: `implementFunctionsOf("derelict.opengl3.functions")` I also tried to make implementFunctionsOf a mixin template.
I forgot to precise, that I don't have a main, because I am trying to create an opengl32.dll. This is why I already have a mixin to inject to function definitions in the root scope.
Ok, it works with the alias, I didn't see the last () in the implementFunctionsOf prototype. Thank you a lot.
Mar 12 2018
parent arturg <var.spool.mail700 gmail.com> writes:
On Monday, 12 March 2018 at 22:34:10 UTC, Xavier Bigand wrote:
 Ok, it works with the alias, I didn't see the last () in the 
 implementFunctionsOf prototype.

 Thank you a lot.
no problem. :)
Mar 12 2018
prev sibling parent arturg <var.spool.mail700 gmail.com> writes:
On Monday, 12 March 2018 at 22:28:30 UTC, Xavier Bigand wrote:
 I forgot to precise, that I don't have a main, because I am 
 trying to create an opengl32.dll. This is why I already have a 
 mixin to inject to function definitions in the root scope.
you have to pass the string as a template arg so make implementFunctionsOf a template: string implementFunctionsOf(string mod)() { } then mix it in somewhere you want: mixin(implementFunctionOf!"std.stdio");
Mar 12 2018
prev sibling parent Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Monday, 12 March 2018 at 22:24:15 UTC, Xavier Bigand wrote:
 mixin(implementFunctionsOf("derelict.opengl3.functions"));


 As string I get the following error:
 ..\src\api_entry.d(16): Error: variable `mod` cannot be read at 
 compile time
 ..\src\api_entry.d(48):        called from here: 
 `implementFunctionsOf("derelict.opengl3.functions")`

 I also tried to make implementFunctionsOf a mixin template.
make the argument to implementFunctionsOf a template arg.
Mar 12 2018