January 02, 2019
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.

This also produces the same output:
writeln(fmt!("a = $a, b = $b, c = $c", c, a, b));

From what I can see, fmt removes the positional relationship between the vars and the format  specifiers, but you still have to supply the actual variables themselves as arguments, which is a redundancy that string interpolation in some other languages don't appear to have.

Jordan

January 02, 2019
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.
January 02, 2019
On Wednesday, 2 January 2019 at 21:52:06 UTC, Jordan Wilson 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.
>
> This also produces the same output:
> writeln(fmt!("a = $a, b = $b, c = $c", c, a, b));
>
> From what I can see, fmt removes the positional relationship between the vars and the format  specifiers, but you still have to supply the actual variables themselves as arguments, which is a redundancy that string interpolation in some other languages don't appear to have.
>
> Jordan

That is correct, I might have been a bit hasty in sharing my little experiment.
January 02, 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
1 2 3 4 5
Next ›   Last »