Remove instance from array

Jolly James via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jul 5 09:04:16 PDT 2017


On Wednesday, 5 July 2017 at 15:56:45 UTC, Igor Shirkalin wrote:
> On Wednesday, 5 July 2017 at 15:48:14 UTC, Jolly James wrote:
>> On Wednesday, 5 July 2017 at 15:44:47 UTC, Igor Shirkalin 
>> wrote:
>>> On Wednesday, 5 July 2017 at 15:30:08 UTC, Jolly James wrote:
>>>>> WhatEver[] q = [];
>>>>>
>>>>> [...]
>>>>>
>>>>> auto i = new WhatEver();
>>>>> q[] = i;
>>>>>
>>>>
>>>> How does one remove that instance 'i'?
>>>
>>> What exactly do you want to remove? After a[]=i your array 
>>> contain a lot of references to 'i'.
>>
>> I would like to know how works: removing
>>  - the first
>>  - and all
>> references to 'i' inside the 'q'.
>
> Perhaps, for all references to i it should look like:
> a = a.filter!(a => a !is i).array;

Thank you! :)


But why a containers so complicated in D?

In C# I would go for a generic List<T>, which would support 
structs and classes, where I simply could call '.Remove(T item)' 
or '.RemoveAt(int index)'. I would know how this works, because 
the method names make sense, the docs are straight forward.

Here in D everything looks like climbing mount everest. When you 
ask how to use D's containers you are recommended to use dynamic 
arrays instead. When you look at the docs for std.algorithm, e.g. 
the .remove section, you get bombed with things like 
'SwapStrategy.unstable', asserts and tuples, but you aren't told 
how to simply remove 1 specific element.


More information about the Digitalmars-d-learn mailing list