On Wednesday, 3 October 2018 at 11:51:01 UTC, Sebastiaan Koppe
wrote:
On Wednesday, 3 October 2018 at 11:01:53 UTC, Chris Katko wrote:
[...]
A combination of static introspection with string mixins does
the trick:
---
enum colors {
reset = "\033[0m",
red = "\033[31m"
}
auto GenerateColorFuncs() {
string result;
static foreach(c; __traits(allMembers, colors))
result ~= "auto "~c~"(string str) { return colors."~c~"
~ str ~ colors.reset; }";
return result;
}
mixin(GenerateColorFuncs());
void main()
{
import std.stdio;
writeln("bla".red);
}
---
Although you might want to replace the string concatenation
with something more performant if used a lot.
The mixin part wouldn't be slowed by strings, right? So the
"slowness" is the invokation part which changes strings and
forces GC allocations, I guess?
What's the alternative to using strings... for strings?