Jump to page: 1 2
Thread overview
[Issue 4287] New: opOpAssign!("~=") for std.array.Appender
Jun 09, 2011
Rob Jacques
Mar 19, 2012
Rob Jacques
Feb 03, 2013
Andrej Mitrovic
Feb 03, 2013
Andrej Mitrovic
Feb 08, 2013
Andrej Mitrovic
June 06, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4287

           Summary: opOpAssign!("~=") for std.array.Appender
           Product: D
           Version: future
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2010-06-06 08:40:02 PDT ---
In std.array.Appender I'd like to use opOpAssign!("~=") instead of the put() member function. Is this possible? This is handy because in some situations I can almost replace a dynamic array with an Appender, keeping the same appends ~= unchanged in the code.

std.array.Appender can even support two more operations (with complexity O(n ln n) or better) that I have found sometimes useful in my D1 code that uses a struct similar to Appender (but this is less important. Such operations can be allowed even if Appender gets implemented for example with a deque data structure):

- length attribute
- opIndex()

An Appender is not an array, so it's better to not support opIndexAssign or opIndexOpAssign, etc. But peeking inside the Appender data structure with an opIndex() can be sometimes useful and avoids converting the Appender to a whole new array (that can be a costly operation if Appender changes its data structure implementation).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 28, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=4287



--- Comment #1 from bearophile_hugs@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: -------
June 09, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=4287


Rob Jacques <sandford@jhu.edu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |alvaro.segura@gmail.com


--- Comment #2 from Rob Jacques <sandford@jhu.edu> 2011-06-08 20:56:27 PDT ---
*** Issue 5791 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 12, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=4287



--- Comment #3 from bearophile_hugs@eml.cc 2012-03-12 05:37:11 PDT ---
See a discussion thread here, where I have suggested to give Appenhder both "put" method and a "~=" operator:

http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=33135

http://forum.dlang.org/thread/jimj6f$1vq$1@digitalmars.com

Adam D. Ruppe:
> Another annoyance is if you have a function that works on
> regular arrays, you probably used ~=.
> But you decide to switch to Appender to try for a speed boost.
> Now you have to change all the usage too, since the
> interfaces are incompatible!

See other messages in the thread.

Adam D. Ruppe, James Miller, Sönke Ludwig and Timon Gehr seem to agree to add the "~=" to Appender.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 19, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=4287


Rob Jacques <sandford@jhu.edu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |sandford@jhu.edu


--- Comment #4 from Rob Jacques <sandford@jhu.edu> 2012-03-19 14:02:41 PDT ---
https://github.com/D-Programming-Language/phobos/pull/502

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 03, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=4287


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich@gmail.com
           Platform|Other                       |All
         AssignedTo|nobody@puremagic.com        |andrej.mitrovich@gmail.com
         OS/Version|Windows                     |All


--- Comment #5 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-02-03 15:20:42 PST ---
Question:

Why was opOpAssign in that pull implemented with returning the 'this' reference? I saw this in TDPL too, but I don't see the benefit of having this compile:

(foo ~= 1) ~= 1;

Anyway as that pull was closed since it did too much I'm taking over this enhancement.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 03, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=4287


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull
            Version|future                      |D2


--- Comment #6 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-02-03 15:41:57 PST ---
https://github.com/D-Programming-Language/phobos/pull/1108

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 04, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=4287



--- Comment #7 from bearophile_hugs@eml.cc 2013-02-03 16:03:05 PST ---
(In reply to comment #5)

> Why was opOpAssign in that pull implemented with returning the 'this' reference? I saw this in TDPL too, but I don't see the benefit of having this compile:
> 
> (foo ~= 1) ~= 1;

Sometimes I like the assignment to return the value, to write:

a = b = c;

But I think the append doesn't need to return a value.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 07, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=4287


Alex Rønne Petersen <alex@lycus.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |alex@lycus.org
         Resolution|                            |FIXED


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 08, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=4287



--- Comment #8 from bearophile_hugs@eml.cc 2013-02-07 16:38:57 PST ---
It seems the length attribute (and opIndex()) didn't get in this patch. I don't
know if they are worth another ER.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
« First   ‹ Prev
1 2