On Sunday, 14 April 2019 at 17:33:51 UTC, Adam D. Ruppe wrote:
That will expand to:

I'm sorry, I skipped a step here. The compiler doesn't look into the mixin string until after it calls mixin, so technically it goes:

static foreach ->
  mixin("void print" ~ 'A' ~ "(int a) {
       writeln(ch, " - ", a);
  }

And then, from there, mixin parses it and links in as if you wrote:

void printA(int a) {
   writeln(ch, " - ", a);
}

and then, since it is still in static foreach, the compiler will do another layer of transforming that `ch` placeholder and we end up with the final generated code:

void printA(int a) {
   writeln('A', " - -", a);
}



In the last post, I skipped that middle step. It doesn't change anything else, but still it is sometimes helpful to understand exactly what is actually happening.

But yeah, same result in the end - `ch` becomes the literal representation of the item being looped over, so 'A', 'B', or 'C' in the example code.

Reply via email to