digitalmars.D.learn - Return types are not matched with method return type.
- RollingCat (60/60) Dec 31 2012 Hi there. I'm new to D language and make a simple software
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (38/94) Dec 31 2012 It is very helpful to see complete code. If I guessed it right, the
- bearophile (4/6) Dec 31 2012 In D "desiredExtension'" is better than "desired_extension".
- RollingCat (2/31) Jan 01 2013 Thanks for your reply. But what about the case of return type?
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (9/10) Jan 01 2013 I am sorry but I don't understand your question.
- RollingCat (3/3) Jan 06 2013 Thank you very much.
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
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
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
Thanks for your reply. But what about the case of return type? Regards.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?
Jan 01 2013
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
Thank you very much. Your answer helped me to solve the problem. Regards
Jan 06 2013