[Issue 4287] opOpAssign!("~=") for std.array.Appender

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Jan 28 14:40:31 PST 2011


http://d.puremagic.com/issues/show_bug.cgi?id=4287



--- Comment #1 from bearophile_hugs at eml.cc 2011-01-28 14:38:22 PST ---
The put() method is not easy to remember (other collections use insert(), etc),
so for me the ~= is simpler to remember. The needed code for Appender, tested a
little:


    /// Adds or appends data to the managed array.
    void opOpAssign(string op:"~", T)(T data)
    {
        this.put(data);
    }


It allows to write:

import std.stdio, std.array;
void main() {
    auto a = appender!(int[]);
    a ~= [1, 2];
    a ~= 3;
    writeln(a.data);
}

----------------------

To define an appender of integers I suggest a syntax like:
auto a = appender!(int);

Instead of:
auto a = appender!(int[]);

because the significant type here is of the items added to the appender. The
fact that Appender uses an array to store such items is an implementation
detail the user must be able to ignore (an Appender may be implemented with a
dynamic array of fixed-sized arrays of items too, like some C++ deque data
structures, to decrease large memory allocations, at the cost of a slower O(n)
"data" method to convert the items in an array).

----------------------

An O(log n) opIndex() too is useful for Appender, it's also useful to avoid
some usages of "data" method.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list