Return types are not matched with method return type.
Ali Çehreli
acehreli at yahoo.com
Mon Dec 31 09:02:59 PST 2012
On 12/31/2012 08:11 AM, RollingCat wrote:
> Hi there. I'm new to D language and make a simple software monitoring
> new file with specified extension.
>
> class FileList
> {
> str_CurrentDir;
> bool ReturnMatchedFile(string str_arg_filename, string str_Extension)
> {
> return endsWith(str_arg_filename, '.'~str_Extension);
> }
>
> void GetCurrentList(string str_arg_Extension)
> {
> auto filter_CurrentList=filter!ReturnMatchedFile(str_arg_Extension,
> dirEntries(str_CurrentDir, SpanMode.shallow));
> }
> }
>
> with this FileList class, after making instance of FileList and
> assigning str_CurrentDir variable, when I call GetCurrentList("d"), it
> says that std.algorithm.filter! ~~ does not match any function template
> declaration.
> OK. So I modified above code like below just for a test.
>
> class FileList
> {
> str_CurrentDir;
> bool ReturnMatchedFile(string str_arg_filename)
> {
> return endsWith(str_arg_filename, ".d");
> }
>
> void GetCurrentList()
> {
> auto
> filter_CurrentList=filter!ReturnMatchedFile(dirEntries(str_CurrentDir,
> SpanMode.shallow));
> }
> }
>
> And it says that
> this for ReturnMatchedFile needs to be type FileList not type
> FileResult!(ReturnMatchedFile, DirIterator).
> I can't understand it!
>
> Anyway, to solve the problem, again, I modified above code like below.
>
> static bool ReturnMatchedFile(string str_arg_filename)
> {
> return endsWith(str_arg_filename, ".d");
> }
>
> class FileList
> {
> str_CurrentDir;
>
> void GetCurrentList()
> {
> auto
> filter_CurrentList=filter!ReturnMatchedFile(dirEntries(str_CurrentDir,
> SpanMode.shallow));
> }
> }
>
> and it works.
>
> What is the problem?
>
> Best regards and happy new year! :)
It is very helpful to see complete code. If I guessed it right, the
following works:
import std.algorithm;
import std.file;
class FileList
{
string str_CurrentDir;
bool ReturnMatchedFile(string str_arg_filename, string str_Extension)
{
return endsWith(str_arg_filename, '.'~str_Extension);
}
void GetCurrentList(string str_arg_Extension)
{
auto filter_CurrentList =
filter!(a => ReturnMatchedFile(a, str_arg_Extension))
(dirEntries(str_CurrentDir, SpanMode.shallow));
}
}
void main()
{
auto f = new FileList();
}
The problem was that you did not provide the template argument
parentheses to filter. You can omit them only if there is only one word.
I have used the lambda syntax as described here:
http://dlang.org/expression.html#Lambda
Each element of dirEntries() appears to the lambda as its 'a' parameter.
That parameter is then applied to the right-hand side of the => operator.
Unrelated to the question, I recommend that you don't use prefixes like
'str_' and 'arg_'; they make the code harder to read. For example, I
would like 'desired_extension' better than 'str_arg_Extension', because
it would carry its meaning better.
Also, predicates like ReturnMatchedFile() are usually named like
hasMatchingExtension().
Ali
--
D Programming Language Tutorial: http://ddili.org/ders/d.en/index.html
More information about the Digitalmars-d-learn
mailing list