Search code examples
tuplesd

D: How create array of Tuples?


I am looking at docs and can't understand hot can I create array of Tuples. It's compile fine:

auto myDataTuple = tuple(url, path);

but this code produce error:

auto myDataTuples [] ~= myDataTuple;

Error: no identifier for declarator myDataTuples[].

It's can't understand type for myDataTuples or what?


Solution

  • You can't append to a declaration since it doesn't exist yet.

    The type tuple(x, y) returns is Tuple!(typeof(x), typeof(y)). You can make an array of them. So if url and path are both strings, try:

    Tuple!(string, string)[] myDataTuple; // the [] makes an array
    myDataTuple ~= tuple(url, path);
    

    PS: it is my opinion that structs are better than tuples basically all the time. (a Tuple is just a generated struct anyway). You can probably also do struct MyData { string url; string path; } and use MyData everywhere too. It is easier to realize what it is later.