www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Mixin and compile-time functions for code generation

reply Cecil Ward <cecil cecilward.com> writes:
I have a function that can be run at compile-time and which will 
be able to output code to be injected into the D source code 
stream. Can I get mixin whatever to do this for me? Mixin with a 
function that runs at compile-time and creates the required 
source ? Like D’s solution for a replacement for function-style C 
preprocessor macros ? - but far more advanced and capable ?

I need to re-read Ali Çehreli’s excellent book for the third time.
Jun 24 2023
next sibling parent reply Adam D Ruppe <destructionator gmail.com> writes:
On Saturday, 24 June 2023 at 17:31:31 UTC, Cecil Ward wrote:
 Can I get mixin whatever to do this for me? Mixin with a 
 function that runs at compile-time and creates the required 
 source ?
have you tried it?
Jun 24 2023
parent Cecil Ward <cecil cecilward.com> writes:
On Saturday, 24 June 2023 at 17:43:52 UTC, Adam D Ruppe wrote:
 On Saturday, 24 June 2023 at 17:31:31 UTC, Cecil Ward wrote:
 Can I get mixin whatever to do this for me? Mixin with a 
 function that runs at compile-time and creates the required 
 source ?
have you tried it?
No, not so far. Adam, if you think that is a workable approach then I certainly will do so. It’s going to be very awkward generating literal strings in D code using literal strings in the meta-mixin-code or whatever I should call it.
Jun 24 2023
prev sibling parent =?UTF-8?Q?Christian_K=c3=b6stlin?= <christian.koestlin gmail.com> writes:
On 24.06.23 18:31, Cecil Ward wrote:
 I have a function that can be run at compile-time and which will be able 
 to output code to be injected into the D source code stream. Can I get 
 mixin whatever to do this for me? Mixin with a function that runs at 
 compile-time and creates the required source ? Like D’s solution for a 
 replacement for function-style C preprocessor macros ? - but far more 
 advanced and capable ?
 
 I need to re-read Ali Çehreli’s excellent book for the third time.
Is this what you want todo: ```d import std.string : format, capitalize; import std.stdio : writeln; string getter(string v) { return "auto get%s() { return %s; }".format(v.capitalize, v); } class T { int abc; mixin(getter("abc")); } void main() { auto t = new T(); writeln(t.getAbc()); } ```
Jun 30 2023