Challenge: write a template-instance free dispatcher for text printing
Richard Andrew Cattermole (Rikki)
richard at cattermole.co.nz
Mon May 25 15:19:51 UTC 2026
I have a challenge for all to try!
For Walter says templates must be avoided in some core code!
So let's see if D can do it, but... There are two rules.
1. You cannot use ANY template instantiations to do the work
itself
2. No string mixins!
Both of these are expensive, and they need to go.
So, can you create dispatch code that:
1. Can identify and handle (in our case, writeln is fine for
this), individual arguments that are not in IES?
2. Can identify arguments that are IES, and then identify and
handle the input of literals and expressions sentinals + the
values that go with the expression sentinal?
To give an idea, here is a simple test program:
```d
import std;
void main()
{
int someVar;
bool aFlag;
string text = "Hi!";
dispatcher("prefix", i" $(someVar)/$(someVar) = $(text) ",
"suffix");
}
void dispatcher(Args...)(auto ref Args args) {
static foreach(arg; args) {
writefln!"%040s: %s"(typeof(arg).stringof, arg);
}
}
```
And its output:
```
string: prefix
InterpolationHeader:
InterpolatedLiteral!" ":
InterpolatedExpression!"someVar":
int: 0
InterpolatedLiteral!"/": /
InterpolatedExpression!"someVar":
int: 0
InterpolatedLiteral!" = ": =
InterpolatedExpression!"text":
string: Hi!
InterpolatedLiteral!" ":
InterpolationFooter:
string: suffix
```
You need to somehow find a way to replace that static foreach, so
that you have the following information:
```
string: prefix
InterpolationHeader:
InterpolatedLiteral!" ":
InterpolatedExpression!"someVar":
int: 0
InterpolatedLiteral!"/": /
InterpolatedExpression!"someVar":
int: 0
InterpolatedLiteral!" = ": =
InterpolatedExpression!"text":
string: Hi!
InterpolatedLiteral!" ":
InterpolationFooter:
string: suffix
```
It is important that you can skip arguments, and handle multiple
at a time. In essence, you need some way to do a for loop but
with enum's and at compile time.
More information about the Digitalmars-d
mailing list