CTFE, string mixins & code generation

Marco de Wild mdwild at sogyo.nl
Fri Jan 24 16:59:53 UTC 2020


On Friday, 24 January 2020 at 16:21:48 UTC, Jan Hönig wrote:
> I am looking for a detailed explanation or showcase regarding 
> CTFE and string mixins.
> I want to play with D a little bit regarding code generation.
> I would like to have a pseudo-AST, consisting of a few classes, 
> to represent some calculation. Think of a loop, some 
> statements, and expressions.
>
> To do that, I want to be certain that this AST can be computed 
> and generated during compile-time, with CTFE and string mixins.
>
> Are there limitations regarding CTFE (GC, global variables, 
> static variables, templates, ....)?
> Are there any limitations regarding string mixins (which are 
> not already included int the CTFE limitations)?

For CTFE: functions should be pure. Therefore you cannot use 
global or static variables. Constants (enums) are perfectly fine 
to use though. I don't know the state of the GC and CTFE. I 
recall that there might be some complexity when using the `new` 
keyword.

https://dlang.org/spec/function.html#interpretation

Basically the only limitation of string mixins is that a single 
string should evaluate to valid D code, opposed to C macros. So
int y mixin("= 6");
doesn't compile, while
int y = mixin("6");
or
mixin("int y = 6;");
does. You can use CTFE to compose the string.


More information about the Digitalmars-d-learn mailing list