www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is this bug ? format %(%)

reply novice3 <sorryno em.ail> writes:
https://run.dlang.io/is/p4NVp8
```d
void main()
{
     import std.stdio: writefln;
     string[] s = ["a", "b", "c"];
     writefln("%(%s, %)", s);
}
```
output
```d
"a", "b", "c"
```
expected
```d
a, b, c
```

there is extra quotes, wich not present in firmat specifier.
is this bug, or i should change something in my code?
Apr 07
parent reply Paul Backus <snarwin gmail.com> writes:
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);
Apr 07
parent reply novice2 <sorryno em.ail> writes:
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
Apr 07
next sibling parent reply Paul Backus <snarwin gmail.com> writes:
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
Apr 07
next sibling parent Berni44 <someone somemail.com> writes:
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.
Apr 07
prev sibling parent Meta <jared771 gmail.com> writes:
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
Apr 07
prev sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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

 =C2=A0=C2=A0=C2=A0 writefln("%-(%s, %)", s);
=20 sorry i dont read docs so carefully thanks
For the sake of completeness, I mention this feature in a couple of=20 other places: http://ddili.org/ders/d.en/formatted_output.html#ix_formatted_output.%= -( https://www.youtube.com/watch?v=3DdRORNQIB2wA&t=3D1146s Ali
Apr 07