www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Get the return type of the function

reply xtreak <tir.karthi gmail.com> writes:
I was reporting a patch for the regression by removing the code 
that was causing the error. The bug was that map was not 
accepting multiple lambdas. It was suggested to check for void 
functions and lambdas. I couldn't find a function to check for 
return type in the std.traits. I tried the explicit for loop thus 
checking for the void functions as in the else case. I am D 
newbie it will be helpful in having the community help me in 
fixing the issue.

foreach(g, i; fun) {
   alias k = unaryFun!(fun[g]);
   static assert(!is(AppliedReturnType!k == void), "Mapping 
function must not return void.");
}

Bug report : https://issues.dlang.org/show_bug.cgi?id=15480
Seems depends on : https://issues.dlang.org/show_bug.cgi?id=5710
Pull request : 
https://github.com/D-Programming-Language/phobos/pull/3963
Introduced as a part of 
https://github.com/D-Programming-Language/phobos/pull/1917
Feb 03 2016
parent reply Meta <jared771 gmail.com> writes:
On Wednesday, 3 February 2016 at 17:12:03 UTC, xtreak wrote:
 I was reporting a patch for the regression by removing the code 
 that was causing the error. The bug was that map was not 
 accepting multiple lambdas. It was suggested to check for void 
 functions and lambdas. I couldn't find a function to check for 
 return type in the std.traits. I tried the explicit for loop 
 thus checking for the void functions as in the else case. I am 
 D newbie it will be helpful in having the community help me in 
 fixing the issue.

 foreach(g, i; fun) {
   alias k = unaryFun!(fun[g]);
   static assert(!is(AppliedReturnType!k == void), "Mapping 
 function must not return void.");
 }

 Bug report : https://issues.dlang.org/show_bug.cgi?id=15480
 Seems depends on : https://issues.dlang.org/show_bug.cgi?id=5710
 Pull request : 
 https://github.com/D-Programming-Language/phobos/pull/3963
 Introduced as a part of 
 https://github.com/D-Programming-Language/phobos/pull/1917
Unles I'm misunderstanding you, you can get the return type of a function by using std.traits.ReturnType: void test() {} static assert(is(ReturnType!test == void));
Feb 03 2016
parent reply xtreak <tir.karthi gmail.com> writes:
On Wednesday, 3 February 2016 at 17:40:23 UTC, Meta wrote:
 On Wednesday, 3 February 2016 at 17:12:03 UTC, xtreak wrote:
 I was reporting a patch for the regression by removing the 
 code that was causing the error. The bug was that map was not 
 accepting multiple lambdas. It was suggested to check for void 
 functions and lambdas. I couldn't find a function to check for 
 return type in the std.traits. I tried the explicit for loop 
 thus checking for the void functions as in the else case. I am 
 D newbie it will be helpful in having the community help me in 
 fixing the issue.

 foreach(g, i; fun) {
   alias k = unaryFun!(fun[g]);
   static assert(!is(AppliedReturnType!k == void), "Mapping 
 function must not return void.");
 }

 Bug report : https://issues.dlang.org/show_bug.cgi?id=15480
 Seems depends on : 
 https://issues.dlang.org/show_bug.cgi?id=5710
 Pull request : 
 https://github.com/D-Programming-Language/phobos/pull/3963
 Introduced as a part of 
 https://github.com/D-Programming-Language/phobos/pull/1917
Unles I'm misunderstanding you, you can get the return type of a function by using std.traits.ReturnType: void test() {} static assert(is(ReturnType!test == void));
Thanks. I was trying to get the return type of lambdas. I was trying the following and got an error. I was using dpaste with dmd 2.070 writeln(ReturnType!(a =(a *a))) Error: template instance f662.main.ReturnType!((a) => a * a) does not match template declaration ReturnType(func...) if (func.length == 1 && isCallable!func)
Feb 03 2016
next sibling parent "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
On Wed, Feb 03, 2016 at 06:40:27PM +0000, xtreak via Digitalmars-d-learn wrote:
[...]
 Thanks. I was trying to get the return type of lambdas. I was trying
 the following and got an error. I was using dpaste with dmd 2.070
 
 writeln(ReturnType!(a =(a *a)))
 
 Error: template instance f662.main.ReturnType!((a) => a * a) does not
 match template declaration ReturnType(func...) if (func.length == 1 &&
 isCallable!func)
Not sure if this will help, but if it's possible to check the return type inside the function, you can use typeof(return): auto func(float x) { return cast(int) x; static assert(is(typeof(return) == int)); } T -- "Outlook not so good." That magic 8-ball knows everything! I'll ask about Exchange Server next. -- (Stolen from the net)
Feb 03 2016
prev sibling parent reply Meta <jared771 gmail.com> writes:
On Wednesday, 3 February 2016 at 18:40:27 UTC, xtreak wrote:
 Thanks. I was trying to get the return type of lambdas. I was 
 trying the following and got an error. I was using dpaste with 
 dmd 2.070

 writeln(ReturnType!(a =(a *a)))

 Error: template instance f662.main.ReturnType!((a) => a * a) 
 does not match template declaration ReturnType(func...) if 
 (func.length == 1 && isCallable!func)
Ah, I see. I'd like to test something; can you please change `(a) => a * a` to `(int a) => a * a` and post the results?
Feb 03 2016
next sibling parent reply sigod <sigod.mail gmail.com> writes:
On Wednesday, 3 February 2016 at 19:21:06 UTC, Meta wrote:
 On Wednesday, 3 February 2016 at 18:40:27 UTC, xtreak wrote:
 Thanks. I was trying to get the return type of lambdas. I was 
 trying the following and got an error. I was using dpaste with 
 dmd 2.070

 writeln(ReturnType!(a =(a *a)))

 Error: template instance f662.main.ReturnType!((a) => a * a) 
 does not match template declaration ReturnType(func...) if 
 (func.length == 1 && isCallable!func)
Ah, I see. I'd like to test something; can you please change `(a) => a * a` to `(int a) => a * a` and post the results?
This works. http://dpaste.dzfl.pl/92c254ef6cf6
Feb 03 2016
parent reply Ivan Kazmenko <gassa mail.ru> writes:
On Wednesday, 3 February 2016 at 22:09:37 UTC, sigod wrote:
 On Wednesday, 3 February 2016 at 19:21:06 UTC, Meta wrote:
 Ah, I see. I'd like to test something; can you please change 
 `(a) => a * a` to
 `(int a) => a * a` and post the results?
This works. http://dpaste.dzfl.pl/92c254ef6cf6
Seems reasonable: `(int a) => a * a` has return type `int` but just `(a) => a * a` does not yet know the type of `a`, and so can not tell the return type.
Feb 03 2016
parent reply xtreak <tir.karthi gmail.com> writes:
On Wednesday, 3 February 2016 at 23:57:12 UTC, Ivan Kazmenko 
wrote:
 On Wednesday, 3 February 2016 at 22:09:37 UTC, sigod wrote:
 On Wednesday, 3 February 2016 at 19:21:06 UTC, Meta wrote:
 Ah, I see. I'd like to test something; can you please change 
 `(a) => a * a` to
 `(int a) => a * a` and post the results?
This works. http://dpaste.dzfl.pl/92c254ef6cf6
Seems reasonable: `(int a) => a * a` has return type `int` but just `(a) => a * a` does not yet know the type of `a`, and so can not tell the return type.
Thanks for the reply. But the issue was about knowing the type of lambda in map. Most people won't enter the type of argument in a map. Is there a way to detect the return type will be non void. I don't need to know the type I just wanr to verify its not void. Yes * can be overloaded and can have different types based on the input having the overloaded operator implemented. I just want to check if the return type is not void.
Feb 03 2016
next sibling parent reply tsbockman <thomas.bockman gmail.com> writes:
On Thursday, 4 February 2016 at 02:06:00 UTC, xtreak wrote:
 On Wednesday, 3 February 2016 at 23:57:12 UTC, Ivan Kazmenko 
 wrote:
 Seems reasonable: `(int a) => a * a` has return type `int` but 
 just `(a) => a * a` does not yet know the type of `a`, and so 
 can not tell the return type.
Thanks for the reply. But the issue was about knowing the type of lambda in map. Most people won't enter the type of argument in a map. Is there a way to detect the return type will be non void. I don't need to know the type I just wanr to verify its not void. Yes * can be overloaded and can have different types based on the input having the overloaded operator implemented. I just want to check if the return type is not void.
See my PR for a workaround: https://github.com/D-Programming-Language/phobos/pull/3969/files
Feb 03 2016
parent reply xtreak <tir.karthi gmail.com> writes:
On Thursday, 4 February 2016 at 05:50:05 UTC, tsbockman wrote:
 On Thursday, 4 February 2016 at 02:06:00 UTC, xtreak wrote:
 On Wednesday, 3 February 2016 at 23:57:12 UTC, Ivan Kazmenko 
 wrote:
 [...]
Thanks for the reply. But the issue was about knowing the type of lambda in map. Most people won't enter the type of argument in a map. Is there a way to detect the return type will be non void. I don't need to know the type I just wanr to verify its not void. Yes * can be overloaded and can have different types based on the input having the overloaded operator implemented. I just want to check if the return type is not void.
See my PR for a workaround: https://github.com/D-Programming-Language/phobos/pull/3969/files
Thanks a lot for the patch. It will be helpful if you could explain the workaround as I am a newbie in D language.
Feb 03 2016
parent tsbockman <thomas.bockman gmail.com> writes:
On Thursday, 4 February 2016 at 07:11:44 UTC, xtreak wrote:
 On Thursday, 4 February 2016 at 05:50:05 UTC, tsbockman wrote:
 See my PR for a workaround: 
 https://github.com/D-Programming-Language/phobos/pull/3969/files
Thanks a lot for the patch. It will be helpful if you could explain the workaround as I am a newbie in D language.
Explanation here: https://github.com/tsbockman/phobos/commit/be7c55fbea7f37987e9f965f968c54b4a3f7342b#commitcomment-15878936
Feb 04 2016
prev sibling parent Meta <jared771 gmail.com> writes:
On Thursday, 4 February 2016 at 02:06:00 UTC, xtreak wrote:
 Thanks for the reply. But the issue was about knowing the type 
 of lambda in map.
Yes, I was just making sure that I knew why ReturnType!lambda was failing, which is that it is a template and does not have a return type until it is instantiated with arguments.
Feb 03 2016
prev sibling parent xtreak <tir.karthi gmail.com> writes:
On Wednesday, 3 February 2016 at 19:21:06 UTC, Meta wrote:
 On Wednesday, 3 February 2016 at 18:40:27 UTC, xtreak wrote:
 Thanks. I was trying to get the return type of lambdas. I was 
 trying the following and got an error. I was using dpaste with 
 dmd 2.070

 writeln(ReturnType!(a =(a *a)))

 Error: template instance f662.main.ReturnType!((a) => a * a) 
 does not match template declaration ReturnType(func...) if 
 (func.length == 1 && isCallable!func)
Ah, I see. I'd like to test something; can you please change `(a) => a * a` to `(int a) => a * a` and post the results?
Thanks for the reply. But the issue was about knowing the type of lambda in map. Most people won't enter the type of argument in a map. Is there a way to detect the return type will be non void. The return type is not much necessary here I just need to verify the function is not void.
Feb 03 2016