digitalmars.D.learn - Get the return type of the function
- xtreak (19/19) Feb 03 2016 I was reporting a patch for the regression by removing the code
- Meta (5/24) Feb 03 2016 Unles I'm misunderstanding you, you can get the return type of a
- xtreak (8/35) Feb 03 2016 Thanks. I was trying to get the return type of lambdas. I was
- H. S. Teoh via Digitalmars-d-learn (11/19) Feb 03 2016 Not sure if this will help, but if it's possible to check the return
- Meta (4/11) Feb 03 2016 Ah, I see. I'd like to test something; can you please change `(a)
- sigod (3/16) Feb 03 2016 This works.
- Ivan Kazmenko (4/10) Feb 03 2016 Seems reasonable: `(int a) => a * a` has return type `int` but
- xtreak (9/21) Feb 03 2016 Thanks for the reply. But the issue was about knowing the type of
- xtreak (6/19) Feb 03 2016 Thanks for the reply. But the issue was about knowing the type of
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
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/1917Unles 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
On Wednesday, 3 February 2016 at 17:40:23 UTC, Meta wrote:On Wednesday, 3 February 2016 at 17:12:03 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)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/1917Unles 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
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
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
On Wednesday, 3 February 2016 at 19:21:06 UTC, Meta wrote:On Wednesday, 3 February 2016 at 18:40:27 UTC, xtreak wrote:This works. http://dpaste.dzfl.pl/92c254ef6cf6Thanks. 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
On Wednesday, 3 February 2016 at 22:09:37 UTC, sigod wrote:On Wednesday, 3 February 2016 at 19:21:06 UTC, Meta 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.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
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: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.On Wednesday, 3 February 2016 at 19:21:06 UTC, Meta 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.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
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:See my PR for a workaround: https://github.com/D-Programming-Language/phobos/pull/3969/filesSeems 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
On Thursday, 4 February 2016 at 05:50:05 UTC, tsbockman wrote:On Thursday, 4 February 2016 at 02:06:00 UTC, xtreak wrote:Thanks a lot for the patch. It will be helpful if you could explain the workaround as I am a newbie in D language.On Wednesday, 3 February 2016 at 23:57:12 UTC, Ivan Kazmenko wrote:See my PR for a workaround: https://github.com/D-Programming-Language/phobos/pull/3969/files[...]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
On Thursday, 4 February 2016 at 07:11:44 UTC, xtreak wrote:On Thursday, 4 February 2016 at 05:50:05 UTC, tsbockman wrote:Explanation here: https://github.com/tsbockman/phobos/commit/be7c55fbea7f37987e9f965f968c54b4a3f7342b#commitcomment-15878936See my PR for a workaround: https://github.com/D-Programming-Language/phobos/pull/3969/filesThanks a lot for the patch. It will be helpful if you could explain the workaround as I am a newbie in D language.
Feb 04 2016
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
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 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.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