www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Removing an element from SList

reply Jesse Phillips <jessekphillips+D gmail.com> writes:
So I'm lost on why it is so hard to get an element removed from an SList:

test.d(7): Error: function std.container.SList!(string).SList.linearRemove
(Range r) is not callable using argument types (string[])
test.d(7): Error: cannot implicitly convert expression (takeOne(["elements"]))
of type string[] to Take!(Range)
test.d(8): Error: function std.container.SList!(string).SList.linearRemove
(Range r) is not callable using argument types (string[])
test.d(8): Error: cannot implicitly convert expression (["elements"]) of type
string[] to Take!(Range)

import std.container;
import std.range;

void main() {
    auto list = ["My", "elements"];
    auto slist = SList!(string)(list);
    slist.linearRemove(takeOne(["elements"]));
    slist.linearRemove(["elements"]);
}

I even tried to follow the pattern used within the unittests, but I still get a
string[] out of take.
May 14 2011
parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On 2011-05-14 09:00, Jesse Phillips wrote:
 So I'm lost on why it is so hard to get an element removed from an SList:
 
 test.d(7): Error: function std.container.SList!(string).SList.linearRemove
 (Range r) is not callable using argument types (string[]) test.d(7):
 Error: cannot implicitly convert expression (takeOne(["elements"])) of
 type string[] to Take!(Range) test.d(8): Error: function
 std.container.SList!(string).SList.linearRemove (Range r) is not callable
 using argument types (string[]) test.d(8): Error: cannot implicitly
 convert expression (["elements"]) of type string[] to Take!(Range)
 
 import std.container;
 import std.range;
 
 void main() {
     auto list = ["My", "elements"];
     auto slist = SList!(string)(list);
     slist.linearRemove(takeOne(["elements"]));
     slist.linearRemove(["elements"]);
 }
 
 I even tried to follow the pattern used within the unittests, but I still
 get a string[] out of take.
linearRemove needs either an SList.Range (well SList!(string).Range in this case) or a Take!(SList!(string).Range). No other range will work. It's not going based off of values. It's going based off of a range over the container itself, and it removes that range from the container. Just like you need an iterator of the correct type for removal in C++, you need a range of the correct type here. So, passing the value "elements" or ["elements"] does you no good at all. You need to get the range from the container itself. So, you'd need to do something like this: import std.algorithm; import std.container; import std.range; void main() { auto list = ["My", "elements"]; auto slist = SList!(string)(list); auto toRemove = find(slist[], "elements"); slist.linearRemove(take(toRemove, 1)); } takeOne should work instead of take with a value of 1, but SList needs to be updated to know about takeOne, and apparently that hasn't happened yet. In any case, the key thing here is that you're _not_ giving linearRemove a generic range of values to remove. You're giving it a range from the container itself to remove. - Jonathan M Davis
May 14 2011