www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Eager dirEntries

reply Andrej Mitrovic <none none.none> writes:
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?
Mar 21 2011
next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
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;
}
Mar 21 2011
prev sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Andrej Mitrovic:

 this won't compile:
 
 string[] entries = array(dirEntries(directory, SpanMode.shallow));
Do you know why? Bye, bearophile
Mar 21 2011
next sibling parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
 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
Mar 21 2011
parent bearophile <bearophileHUGS lycos.com> writes:
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
Mar 21 2011
prev sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
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?
Mar 21 2011