digitalmars.D.learn - Eager dirEntries
- Andrej Mitrovic (10/10) Mar 21 2011 Currently we have DirEntries, which can be used in a foreach loop. We al...
- Andrej Mitrovic (10/10) Mar 21 2011 Well anywho I've wrapped it in my code:
- bearophile (4/7) Mar 21 2011 Do you know why?
- Jonathan M Davis (6/12) Mar 21 2011 Yes. With dirEntries, you have to tell it the iteration type. It could b...
- bearophile (5/10) Mar 21 2011 Thank you for your answer and I see, you may mean to optionally give it ...
- Andrej Mitrovic (9/16) Mar 21 2011 build.d(164): Error: template std.array.array(Range) if
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
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
Andrej Mitrovic:this won't compile: string[] entries = array(dirEntries(directory, SpanMode.shallow));Do you know why? Bye, bearophile
Mar 21 2011
Andrej Mitrovic: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 Davisthis won't compile: string[] entries = array(dirEntries(directory, SpanMode.shallow));Do you know why?
Mar 21 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
Mar 21 2011
On 3/22/11, bearophile <bearophileHUGS lycos.com> wrote:Andrej Mitrovic: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?this won't compile: string[] entries = array(dirEntries(directory, SpanMode.shallow));Do you know why? Bye, bearophile
Mar 21 2011