Thread overview
Is this bug ? format %(%)
Apr 07, 2021
novice3
Apr 07, 2021
Paul Backus
Apr 07, 2021
novice2
Apr 07, 2021
Paul Backus
Apr 07, 2021
Berni44
Apr 07, 2021
Meta
Apr 07, 2021
Ali Çehreli
April 07, 2021

https://run.dlang.io/is/p4NVp8

void main()
{
    import std.stdio: writefln;
    string[] s = ["a", "b", "c"];
    writefln("%(%s, %)", s);
}

output

"a", "b", "c"

expected

a, b, c

there is extra quotes, wich not present in firmat specifier.
is this bug, or i should change something in my code?

April 07, 2021

On Wednesday, 7 April 2021 at 13:31:59 UTC, novice3 wrote:

>

there is extra quotes, wich not present in firmat specifier.
is this bug, or i should change something in my code?

This is actually an intentional feature (albeit kind of a stupid one). From the documentation:

>

Inside a compound format specifier, strings and characters are escaped automatically. To avoid this behavior, add '-' flag to "%(".

So, you should change your code to

writefln("%-(%s, %)", s);
April 07, 2021

On Wednesday, 7 April 2021 at 13:43:18 UTC, Paul Backus wrote:

>

So, you should change your code to

writefln("%-(%s, %)", s);

sorry i dont read docs so carefully
thanks

April 07, 2021

On Wednesday, 7 April 2021 at 17:04:56 UTC, novice2 wrote:

>

On Wednesday, 7 April 2021 at 13:43:18 UTC, Paul Backus wrote:

>

So, you should change your code to

writefln("%-(%s, %)", s);

sorry i dont read docs so carefully
thanks

It's not your fault--this is a pretty obscure feature, and it's not documented very well. Even after you've found the correct page in the documentation (the page for formattedWrite [1]), you have to scroll down past multiple examples to find the text that explains it.

[1] https://dlang.org/phobos/std_format.html#formattedWrite

April 07, 2021
On 4/7/21 10:04 AM, novice2 wrote:
> On Wednesday, 7 April 2021 at 13:43:18 UTC, Paul Backus wrote:
>> So, you should change your code to
>>
>>     writefln("%-(%s, %)", s);
> 
> sorry i dont read docs so carefully
> thanks

For the sake of completeness, I mention this feature in a couple of other places:

  http://ddili.org/ders/d.en/formatted_output.html#ix_formatted_output.%-(

  https://www.youtube.com/watch?v=dRORNQIB2wA&t=1146s

Ali

April 07, 2021

On Wednesday, 7 April 2021 at 17:31:09 UTC, Paul Backus wrote:

>

It's not your fault--this is a pretty obscure feature, and it's not documented very well. Even after you've found the correct page in the documentation (the page for formattedWrite [1]), you have to scroll down past multiple examples to find the text that explains it.

[1] https://dlang.org/phobos/std_format.html#formattedWrite

The docs of std.format are currently under strong revision, about 90% is already done, but that "feature" is among the other 10%. I hope to get that ready till the end of the month, so the next stable release will have better docs.

Unfortunately it's much more difficult to change that strange behavior without breaking code. Currently I hope for Phobos 2.0 coming soon and meanwhile try to prepare std.format for that change.

April 07, 2021

On Wednesday, 7 April 2021 at 17:31:09 UTC, Paul Backus wrote:

>

On Wednesday, 7 April 2021 at 17:04:56 UTC, novice2 wrote:

>

On Wednesday, 7 April 2021 at 13:43:18 UTC, Paul Backus wrote:

>

So, you should change your code to

writefln("%-(%s, %)", s);

sorry i dont read docs so carefully
thanks

It's not your fault--this is a pretty obscure feature, and it's not documented very well. Even after you've found the correct page in the documentation (the page for formattedWrite [1]), you have to scroll down past multiple examples to find the text that explains it.

[1] https://dlang.org/phobos/std_format.html#formattedWrite

I have created a pull request that will hopefully make this more prominent on the doc page:
https://github.com/dlang/phobos/pull/7944