digitalmars.D - Templated Lists
- Jolly James (5/5) Jun 16 2017 I know that there are arrays, some strange container classes and
- Jonathan M Davis via Digitalmars-d (21/26) Jun 16 2017 AliasSeq is used to create a list of items - e.g. AliasSeq!(int, float)....
- Cym13 (8/13) Jun 16 2017 I'm unclear about your intent: do you want a compile-time list or
- Jolly James (2/14) Jun 16 2017
- Stefan Koch (2/16) Jun 16 2017 just use d's dynamic arrays :)
- =?UTF-8?Q?Ali_=c3=87ehreli?= (11/14) Jun 16 2017 std.variant.Variant perhaps?
- Jolly James (4/19) Jun 16 2017 Thx, but I do not need to mix different types (using variant).
- =?UTF-8?Q?Ali_=c3=87ehreli?= (12/16) Jun 16 2017 With std.algorithm.remove:
- Jolly James (7/21) Jun 16 2017 C#'s generics allow to specify one datatype T that is used, some
- Jonathan M Davis via Digitalmars-d (6/22) Jun 16 2017 I see that I completely misunderstood the question then. I thought that ...
I know that there are arrays, some strange container classes and so on. But how does one create and use a templated list in D that supports adding, removing and sorting items? One that can be used for structs and for classes?
Jun 16 2017
On Friday, June 16, 2017 19:07:49 Jolly James via Digitalmars-d wrote:I know that there are arrays, some strange container classes and so on. But how does one create and use a templated list in D that supports adding, removing and sorting items? One that can be used for structs and for classes?AliasSeq is used to create a list of items - e.g. AliasSeq!(int, float). It can hold types, expressions, or both. It's basically what you get when you have a template argument list or parameter list or a function argument list or parameter list - hence why it can hold a bit of a hodepodge of things and why giving it a good name is rather difficult (its current name being short for alias sequence). It used to be called TypeTuple but was renamed because it doesn't only hold types, and it isn't really a tuple (it auto expands, so you can't really nest them - e.g. AliasSeq!(int, float) and AliasSeq!(int, AliasSeq!float) are going to end up being the same thing). Confusingly, the language documentation tends to call it Tuple (not to be confused with std.typecons.Tuple), even though it isn't really a tuple. But that largely comes down to the fact that no one has a good name for the thing. std.meta provides a variety of templates for operating on AliasSeqs with predicates and whatnot, so it would be the main place to look if you want to operate on an AliasSeq: http://dlang.org/phobos/std_meta.html And of course, std.traits helps with some of the building blocks - particularly for creating compile-time predicates: http://dlang.org/phobos/std_traits.html - Jonathan M Davis
Jun 16 2017
On Friday, 16 June 2017 at 19:07:49 UTC, Jolly James wrote:I know that there are arrays, some strange container classes and so on. But how does one create and use a templated list in D that supports adding, removing and sorting items? One that can be used for structs and for classes?I'm unclear about your intent: do you want a compile-time list or a parameterized list (but that can be used at runtime)? In the first case the answer above is what you want, in the second I recommend reading about templates, maybe http://nomad.so/2013/07/templates-in-d-explained/ Whatever your use case such questions find better answers in the "Learn" section of this forum :)
Jun 16 2017
On Friday, 16 June 2017 at 19:42:44 UTC, Cym13 wrote:I'm unclear about your intent: do you want a compile-time listinterface IList<T> { int Count { get; } void Add(T item); void Remove(T item); T this[int index]; // [...] }
Jun 16 2017
On Friday, 16 June 2017 at 23:08:58 UTC, Jolly James wrote:On Friday, 16 June 2017 at 19:42:44 UTC, Cym13 wrote:just use d's dynamic arrays :)I'm unclear about your intent: do you want a compile-time listinterface IList<T> { int Count { get; } void Add(T item); void Remove(T item); T this[int index]; // [...] }
Jun 16 2017
On 06/16/2017 04:08 PM, Jolly James wrote:On Friday, 16 June 2017 at 19:42:44 UTC, Cym13 wrote:std.variant.Variant perhaps? https://dlang.org/phobos/std_variant.html import std.variant; void main() { Variant[] arr; arr ~= Variant(42); arr ~= Variant("hello"); } Also consider Algebraic in that module. AliI'm unclear about your intent: do you want a compile-time list
Jun 16 2017
On Friday, 16 June 2017 at 23:38:46 UTC, Ali Çehreli wrote:On 06/16/2017 04:08 PM, Jolly James wrote:Thx, but I do not need to mix different types (using variant). Assuming I use simply an dynamic array, how does one remove an specific item?On Friday, 16 June 2017 at 19:42:44 UTC, Cym13 wrote:std.variant.Variant perhaps? https://dlang.org/phobos/std_variant.html import std.variant; void main() { Variant[] arr; arr ~= Variant(42); arr ~= Variant("hello"); } Also consider Algebraic in that module. AliI'm unclear about your intent: do you want a compile-time list
Jun 16 2017
On 06/16/2017 05:02 PM, Jolly James wrote:Thx, but I do not need to mix different types (using variant).Assuming I use simply an dynamic array, how does one remove an specific item?With std.algorithm.remove: import std.algorithm; void main() { auto arr = [ 1, 2, 3, 4, 5, 6 ]; arr = arr.remove(5); // Removes element at index 3 assert(arr == [ 1, 2, 3, 4, 5 ]); arr = arr.remove!(e => e % 2); // Removes elements with odd values assert(arr == [ 2, 4 ]); } Ali
Jun 16 2017
On Saturday, 17 June 2017 at 00:28:50 UTC, Ali Çehreli wrote:On 06/16/2017 05:02 PM, Jolly James wrote: exactly that. :)kind of similar to a template. So, a generic list can contain only elements of type T. That what makes it generic is that M$ had to implement this class only once and now it can be used with any datatype by simply specifying it ( → List<int> ).With std.algorithm.remove: import std.algorithm; void main() { auto arr = [ 1, 2, 3, 4, 5, 6 ]; arr = arr.remove(5); // Removes element at index 3 assert(arr == [ 1, 2, 3, 4, 5 ]); arr = arr.remove!(e => e % 2); // Removes elements with odd values assert(arr == [ 2, 4 ]); }Thank you very much :)
Jun 16 2017
On Friday, June 16, 2017 23:08:58 Jolly James via Digitalmars-d wrote:On Friday, 16 June 2017 at 19:42:44 UTC, Cym13 wrote:I see that I completely misunderstood the question then. I thought that you were trying to do stuff with lists at compile time. For that, I'd just dynamic arrays, but there's also DList in std.container, and http://code.dlang.org/packages/emsi_containers has several containers. - Jonathan M DavisI'm unclear about your intent: do you want a compile-time listinterface IList<T> { int Count { get; } void Add(T item); void Remove(T item); T this[int index]; // [...] }
Jun 16 2017