Interpolated strings

Kapps via Digitalmars-d digitalmars-d at puremagic.com
Fri Apr 21 15:43:22 PDT 2017


On Thursday, 20 April 2017 at 18:28:30 UTC, Atila Neves wrote:
> On Monday, 17 April 2017 at 19:38:33 UTC, Kapps wrote:
>> On Saturday, 15 April 2017 at 20:04:13 UTC, Jonas Drewsen 
>> wrote:
>>> [...]
>>
>> C# got this feature recently. I didn't expect it to be a 
>> significant difference, but I do find it a substantial 
>> improvement. Not only does it clearly show what goes where, 
>> but it's so much cleaner and more convenient.
>
> I don't understand how
>
> writeln($"{a} times 3 is {a * 3}");
>
> is even marginally better than
>
> writeln(a, " times 3 is ", a * 3);  // look ma, works right now!
>
> It's not even fewer characters.
>
> Atila

I find the first one to be cleaner honestly. It shows exactly 
where values are coming from, doesn't have a bunch of commas that 
can be annoying to match up, and doesn't start and stop a string 
multiple times. And for the second one, that's only because 
writeln specifically includes overloads to take in multiple 
objects and merge them together. This isn't something functions 
should have to always do (except in the cases where taking in 
multiple arguments is actually more efficient, like presumably 
with writeln since it doesn't need to actually merge the string).

It's not like it's a huge missing feature. Having something like 
the below isn't too bad:
foo(format("Value of ", a, " * 3 is ", a * 3, "."));
But it's just not as nice as:
foo($"Value of {a} * 3 is {a * 3}.");


More information about the Digitalmars-d mailing list