CTFE, string mixins & code generation

H. S. Teoh hsteoh at quickfur.ath.cx
Fri Jan 24 18:43:14 UTC 2020


On Fri, Jan 24, 2020 at 04:21:48PM +0000, Jan Hönig via Digitalmars-d-learn 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, ....)?

CTFE in general cannot use global variables.  Any state you need must be
created inside a CTFE function, and accessed from within that calling
context.  You *can* assign values produced by CTFE to compile-time
symbols via 'enum', but there are limitations (enums cannot take AA's or
class objects as values, also, once assigned they are immutable).


> Are there any limitations regarding string mixins (which are not
> already included int the CTFE limitations)?

As long as you can produce a string (via CTFE or otherwise) whose value
is known at compile-time and represents valid D code, it's fair game for
string mixins.  However, be aware that there might be scoping and order
of declaration issues (they should be rare, but you might perchance run
into them).

What is your use case?  Maybe describing a bit more details will help us
help you.

P.S. If you want to know more about what CTFE / templates can or cannot
do, and how they interact, you might find this article helpful:

	https://wiki.dlang.org/User:Quickfur/Compile-time_vs._compile-time


T

-- 
In theory, there is no difference between theory and practice.


More information about the Digitalmars-d-learn mailing list