www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Return types are not matched with method return type.

reply "RollingCat" <rollingcat hotmail.com> writes:
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! :)
Dec 31 2012
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Dec 31 2012
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Ali Çehreli:

 I would like 'desired_extension' better than 
 'str_arg_Extension', because it would carry its meaning better.
In D "desiredExtension'" is better than "desired_extension". Bye, bearophile
Dec 31 2012
prev sibling parent reply "RollingCat" <rollingcat hotmail.com> writes:
 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?
Thanks for your reply. But what about the case of return type? Regards.
Jan 01 2013
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 01/01/2013 04:40 PM, RollingCat wrote:

 Thanks for your reply. But what about the case of return type?
I am sorry but I don't understand your question. Even though you said "and it works" in your original post, the code does not compile. (For example, the line "str_CurrentDir;" looks like a member variable definition but it doesn't have a type!) It looks like my guess at what you were trying to do has been wrong. It is very important that you show us what you are trying to do with a piece of real code. Ali
Jan 01 2013
parent "RollingCat" <RollingCat hotmail.com> writes:
Thank you very much.
Your answer helped me to solve the problem.

Regards
Jan 06 2013