The state of string interpolation
Paul Backus
snarwin at gmail.com
Thu Dec 6 22:53:26 UTC 2018
On Thursday, 6 December 2018 at 22:34:14 UTC, o wrote:
> Just think how much nicer this is (lowering to a string):
> `foo(i"My name is $name");`
>
> Compared to this (lowering to a tuple, requiring a conversion
> to a string):
> ```
> import std.conv: text;
> foo(text(i"My name is $name"));
> ```
You can write `foo` in such a way that the user never has to
manually insert a call to `text`:
void foo(string s)
{
// do whatever
}
void foo(Args...)(Args args)
{
import std.conv: text;
foo(text(args));
}
In fact, you can even encapsulate this pattern in a mixin:
template interpArgs(alias fun)
{
import std.format: format;
enum interpArgs = q{
auto %1$s(Args...)(Args args)
{
import std.conv: text;
return %1$s(text(args));
}
}.format(__traits(identifier, fun));
}
mixin(interpArgs!foo);
More information about the Digitalmars-d
mailing list