CTFE, string mixins & code generation

Jan Hönig hrominium at gmail.com
Fri Jan 24 22:47:49 UTC 2020


On Friday, 24 January 2020 at 18:43:14 UTC, H. S. Teoh wrote:
> 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).
>
> 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).

So as long as i don't have some side effects (aka global state), 
i am good to go with anything. Only trouble might be the output. 
But my output has to be strings, so i am good to go.

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

In the area of my research, we do quite a lot of code generation.
We have some kind of DSL (internal or external) and we generate 
performant code.
Usually we have some "generation" language (e.g. Python) and some 
target language (e.g. C++). The project of my colleague (and my 
master's thesis) is pystencils: 
https://pypi.org/project/pystencils/
A stencil code generator in Python which produces C/C++ code.
It doesn't need to be in Python it can be anything (another 
colleague does something similar in Scala)
Of course there are other approaches. If you have a language 
which performs well on its own, you don't need to know 2 
languages (source and target). There are languages like Common 
Lisp, which have strong (well in case of common lisp best) 
macros, and are still fast enough.
D might be another candidate.
I am just curious and wanted to toy with it a little.

> 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

This article is super helpful, let me reed it and play with it a 
little, before you sacrifice your time more with me :)
I'll come back with more questions soon enough.



More information about the Digitalmars-d-learn mailing list