On Wednesday, 14 March 2018 at 15:50:13 UTC, Jonathan Marler
wrote:
On Wednesday, 15 March 2017 at 13:50:28 UTC, Inquie wrote:
[...]
I've got a PR for dmd (https://github.com/dlang/dmd/pull/7988)
that implements "interpolated strings" which makes generating
code with strings MUCH nicer, i.e.
string generateFunction(string attributes, string returnType,
string name, string args, string body)
{
import std.conv : text;
return text(iq{
// This is an interpolated string!
$(attributes) $(returnType) $(name)($(args))
{
$(body)
}
});
}
// Let's use it:
mixin(generateFunction("pragma(inline)", "int", "add", "int a,
int b", "return a + b;"));
assert(100 == add(25, 75));
If you do that give the closing brace a suffix