Token strings as "cheap" macros.
Chris Nicholson-Sauls
ibisbasenji at gmail.com
Thu Sep 6 14:37:56 PDT 2007
Looking at D/2.x Token Strings (q{...}) something came to mind. If we had a way of
embedding expressions which evaluate to strings within this syntax, it could be used to
write rather clean mixins. (The use of which I assume to be a driver behind their
introduction anyhow.)
What I'm thinking of, is this sort of thing:
template Gettor (T, char[] name) {
const Gettor =
T.stringof~" get"~name~" () {
return _"~name~";
}
";
}
Looking more like this:
template Gettor (T, char[] name) {
const Gettor = q{
${T.stringof} get${name} () {
return _${name};
}
};
}
Which benefits from being easier to visually parse (and therefore develop and maintain),
and being open to editor highlighting. I use `` strings right now, with `~...~` embeds,
to similar effect only because my main editor doesn't know about `` strings. Its really
just a somewhat limited builtin compile-time-string-formatting capability.
The syntax doesn't matter, I just borrowed ${} from PHP.
function Gettor ($name) {
return <<<EOI
function get${name} () {
return _${name};
}
EOI;
}
-- Chris Nicholson-Sauls
More information about the Digitalmars-d
mailing list