Thread overview
Eager dirEntries
Mar 21, 2011
Andrej Mitrovic
Mar 21, 2011
Andrej Mitrovic
Mar 21, 2011
bearophile
Mar 21, 2011
Jonathan M Davis
Mar 22, 2011
bearophile
Mar 21, 2011
Andrej Mitrovic
March 21, 2011
Currently we have DirEntries, which can be used in a foreach loop. We also have listDir, which returns a string[] with all entries found in a path. listDir is scheduled for deprecation, so I'm not using it.

DirEntries is definitely more flexible, but I can't eagerly construct an array of strings from it. For example, this won't compile:

string[] entries = array(dirEntries(directory, SpanMode.shallow));

Instead I have to expand the code to this:
string[] entries;
foreach (string name; dirEntries(directory, SpanMode.shallow))
{
    entries ~= name;
}

That's just a waste of precious space. Would it be a good idea to make a feature request for this?
March 21, 2011
Well anywho I've wrapped it in my code:

string[] arrayDirEntries(string path, SpanMode spanMode)
{
    string[] result;
    foreach (string name; dirEntries(path, spanMode))
    {
        result ~= name;
    }
    return result;
}
March 21, 2011
Andrej Mitrovic:

> this won't compile:
> 
> string[] entries = array(dirEntries(directory, SpanMode.shallow));

Do you know why?

Bye,
bearophile
March 21, 2011
> Andrej Mitrovic:
> > this won't compile:
> > 
> > string[] entries = array(dirEntries(directory, SpanMode.shallow));
> 
> Do you know why?

Yes. With dirEntries, you have to tell it the iteration type. It could be either a DirEntry or a string. As such, it fails the template constraint for array. It would probably be possible to extend array to work with it (with you giving it the iteration type as a template argument), but array would have to be reworked a bit for that to work.

- Jonathan M Davis
March 21, 2011
On 3/22/11, bearophile <bearophileHUGS@lycos.com> wrote:
> Andrej Mitrovic:
>
>> this won't compile:
>>
>> string[] entries = array(dirEntries(directory, SpanMode.shallow));
>
> Do you know why?
>
> Bye,
> bearophile
>

build.d(164): Error: template std.array.array(Range) if
(isIterable!(Range) && !isNarrowString!(Range)) does not match any
function template declaration
build.d(164): Error: template std.array.array(Range) if
(isIterable!(Range) && !isNarrowString!(Range)) cannot deduce template
function from argument types !()(DirIterator)

I'm not sure why array() has this signature. Perhaps it needs an update and let any iterable range through?
March 22, 2011
Jonathan M Davis:

> Yes. With dirEntries, you have to tell it the iteration type. It could be either a DirEntry or a string. As such, it fails the template constraint for array. It would probably be possible to extend array to work with it (with you giving it the iteration type as a template argument), but array would have to be reworked a bit for that to work.

Thank you for your answer and I see, you may mean to optionally give it the item type:

string[] entries = array!string(dirEntries(directory, SpanMode.shallow));

Bye,
bearophile