Bug 21808 – std.format: It should be possible to change the order of key and value of AAs.
Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2021-04-07T22:49:25Z
Last change time
2021-05-05T07:12:46Z
Keywords
pull
Assigned to
No Owner
Creator
Bolpat
Comments
Comment #0 by qs.il.paperinik — 2021-04-07T22:49:25Z
When formatting AAs, one can use %(..%|..%) where the left side formats key--value pairs using two format specifiers. Here, positional formatting of the form %1$..s does not do its job. Position arguments are ignored.
Test case:
import std.stdio;
auto spelled = [ 1 : "one", 10 : "ten", 100 : "hundred" ];
// expected: prints, in any order, 1 (one), 10 (ten), 100 (hundred)
// result: prints, in any order, 1 (one), 10 (ten), 100 (hundred) [ok]
writefln("%-(%1$s (%2$s)%|, %)", spelled);
// expected: prints, in any order, one (1), ten (10), hundred (100)
// result: prints, in any order, 1 (one), 10 (ten), 100 (hundred) [fail]
writefln("%-(%2$s (%1$s)%|, %)", spelled);
Changing this to "enhancement", because normal use of positional parameters has a completely different meaning - it gives the position of an argument in the function call and has nothing to do with the order of the elements of a single argument.
Anyway, I see the need for the possibility to change the order; and positional arguments don't make any sense inside of a compound specifier. Therefore I think, it is OK to (ab)use them like suggested here. But then it's not a bug, but an enhancement.
Comment #3 by dlang-bot — 2021-04-29T15:46:00Z
@berni44 created dlang/phobos pull request #8020 "Fix Issue 21808 - std.format: It should be possible to change the order of key and value of AAs." fixing this issue:
- Fix Issue 21808 - std.format: It should be possible to change the order of key and value of AAs.
https://github.com/dlang/phobos/pull/8020
Comment #4 by qs.il.paperinik — 2021-04-29T18:52:31Z
(In reply to Berni44 from comment #2)
> Changing this to "enhancement", because normal use of positional parameters
> has a completely different meaning - it gives the position of an argument in
> the function call and has nothing to do with the order of the elements of a
> single argument.
>
> Anyway, I see the need for the possibility to change the order; and
> positional arguments don't make any sense inside of a compound specifier.
> Therefore I think, it is OK to (ab)use them like suggested here. But then
> it's not a bug, but an enhancement.
For tuples' %( %) expansion format, reordering works:
import std.typecons, std.stdio;
writefln("%( %2$s (%1$s) %)", tuple(1, "one"));
// prints: one (1)
Comment #5 by bugzilla — 2021-04-30T10:14:58Z
(In reply to Bolpat from comment #4)
> For tuples' %( %) expansion format, reordering works:
>
> import std.typecons, std.stdio;
> writefln("%( %2$s (%1$s) %)", tuple(1, "one"));
> // prints: one (1)
I'd guess, that this was unintended (there is no unittest containing this, nor was it mentioned in the PR introducing this) and should be considered a bug (or an unknown feature?).
Compare:
writefln("%( %2$s (%1$s) %|%)", tuple(1, "one"));
Now it throws. The reason, why the first version works is, because under the hood it is transformed to something similar to
writefln(" %2$s (%1$s) ", 1, "one");
And now you've got the elements of the tuple as arguments.
Anyway, as I wrote above and in the PR: I like the idea. Let's see, if it will be accepted.
Comment #6 by dlang-bot — 2021-05-05T07:12:46Z
dlang/phobos pull request #8020 "Fix Issue 21808 - std.format: It should be possible to change the order of key and value of AAs." was merged into master:
- fbc4c53d7bd1916d5253c6d4a7bd2d61b80d31c3 by berni44:
Fix Issue 21808 - std.format: It should be possible to change the order of key and value of AAs.
https://github.com/dlang/phobos/pull/8020