Bug 5791 – array.Appender with operator "~=" (source included)
Status
RESOLVED
Resolution
DUPLICATE
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-03-28T13:21:00Z
Last change time
2011-06-08T20:56:26Z
Assigned to
nobody
Creator
alvaro.segura
Comments
Comment #0 by alvaro.segura — 2011-03-28T13:21:14Z
For consistency with normal array appending and to better follow "the D way" it would be good to have a "~=" operator in struct std.array.Appender.
Operators are easier to remember and it will be easier to change existing code using normal arrays to Appender when performance suggests it would improve.
Just add the following in the body of struct Appender in phobos/std/array.d and it's done:
void opOpAssign(string op, U)(U u)
{
static if (op=="~")
{
put(u);
}
}
It works with ranges too:
Appender!(int[]) a; // this can be changed to int[] a; with no harm!
a ~= 4;
a ~= [5, 7];
Additionally, for more consistency and ease of change T[] <-> Appender!(T[]), index operator can also be added:
auto opIndex(size_t i)
{
return data[i];
}
So we can now:
writeln(a[2]); // will print 7
Comment #1 by sandford — 2011-06-08T20:56:26Z
*** This issue has been marked as a duplicate of issue 4287 ***