Better string mixins

Steven Schveighoffer schveiguy at gmail.com
Wed Jan 2 21:59:19 UTC 2019


On 1/2/19 4:54 PM, Dgame wrote:
> On Wednesday, 2 January 2019 at 21:06:19 UTC, Bastiaan Veelo wrote:
>> On Wednesday, 2 January 2019 at 18:59:53 UTC, Dgame wrote:
>>> Hi, I've played around with string interpolation and wanted to ask if 
>>> this is somewhat helpful?
>>>
>>> https://run.dlang.io/is/6AokiH
>>
>> You are aware of `writefln`?
>>
>>>    writeln(fmt!("a = $a, b = $b, c = $c", a, b, c));
>>>    writefln("a = %s, b = %s, c = %s", a, b, c);
>>
>> These lines produce the same output.
> 
> Yes I do. But consider this:
> 
> int c = 3;
> int z = c * c;
> 
> writefln("%s * %s = %s", c, c, z);
> 
> vs
> 
> writeln(fmt!("$c * $c = $z", c, z));
> 
> Just one c is applied. But I've disovered one slightly error here, the 
> interpolation variables should be enclosed with { to prevent mistakenly 
> replacement.
> 
> Also, currently no further "magic" as ${c * 2} can't be interpreted as 
> someone might wish, it is somewhat static, but I've thought it might be 
> interesting.

It's better, but still is not DRY enough. I still have to repeat the 
variable name. In terms of string interpolation, the point is to put the 
reference right at the usage, not to repeat it elsewhere. And as you 
alluded to, it's very key for string interpolation to allow arbitrary 
expressions.

-Steve


More information about the Digitalmars-d mailing list