Are there an equivalent to C#'s List in D's stdlib?
James Blachly
james.blachly at gmail.com
Fri Jan 8 03:06:24 UTC 2021
On 1/7/21 9:53 PM, Jack wrote:
> I coduln't find an equivalent in the documentation, I could see
> appender, Array, container etc but none of has a Remove(T item) method
> like C#'s [1]. Are there not such implementation and I do have to write
> one myself or I just couldn't find?
>
> [1]:
> https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.remove?view=net-5.0
>
Speaking for me personally, I would try to work with Ranges[0], and in a
functional style simply filter [1] out in the same way one calls
`Remove` in C#.
for example:
```D-ish
auto someRangeType = functionThatDoesWhatever();
auto rangeWithItemRemoved = someRangeType.filter(x => x.prop != whatever);
```
In the C# docs, Remove<T> is called on a class which has overloaded
`Equals` such that the call to `Remove(new Part(){PartId=1534,
PartName="cogs"});` considers only the PartId and does not match on name.
Again, you could `filter(x => x.PartId != 1534)`, or you could, as in
C#, overload the comparison operator for the class/struct you are
comparing, then `filter(x => x != new Part(1534, "cogs"))`
Finally, although you asked about the standard library, there are many
other container libraries, like emsi-containers [2] which do have List
types that implement a `remove` function just as in C#
[0] http://ddili.org/ders/d.en/ranges.html
[1] https://dlang.org/phobos/std_algorithm_iteration.html#filter
[2] https://dlang-community.github.io/containers/index.html
More information about the Digitalmars-d-learn
mailing list