On Tuesday, 20 April 2021 at 15:37:25 UTC, Brian wrote:
>I suppose what I want to do is traverse the compiler's transformation of the mixin. The mixin page suggests it performs that work at semantic evaluation time.
~Brian
I try to explain how it does that.
First off, as the article says, a mixin must always expand to either a complete statement/declaration, or to an expression. This let's the compiler to complete the grammar pass (of stuff outside the mixin) before the semantic analysis, without needing to worry about the content of the mixin. This is why mixin("{")
won't work - the grammar pass would have to analyze what is in the string to understand that.
Secondly, a mixin can accept any string available at compile time. D has a compile time function execution engine, that is used if a result of a function is needed at compile time. This is the case for mixin
arguments, but it's not the only thing. Behold:
import std;
//a regular function, callable at both runtime and compile time
string makeSymbol(const char c){return "in" ~ c;}
//a template
alias Type(T) = T;
//compile-time constant that behaves like a variable
enum intStr = makeSymbol('t');
void main()
{
//same as int x = 15;
//all work to determine the type done by CTFE engine at semantic pass
Type!(mixin(intStr)) x = 15;
x += 10;
x.writeln;
}