Jump to page: 1 2
Thread overview
Get the return type of the function
Feb 03, 2016
xtreak
Feb 03, 2016
Meta
Feb 03, 2016
xtreak
Feb 03, 2016
H. S. Teoh
Feb 03, 2016
Meta
Feb 03, 2016
sigod
Feb 03, 2016
Ivan Kazmenko
Feb 04, 2016
xtreak
Feb 04, 2016
tsbockman
Feb 04, 2016
xtreak
Feb 04, 2016
tsbockman
Feb 04, 2016
Meta
Feb 04, 2016
xtreak
February 03, 2016
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
February 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/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));
February 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:
>> 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)
February 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)
February 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?
February 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. 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
February 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:
>> 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.
February 04, 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. 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.
February 04, 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:
>> 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.
February 04, 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:
>> 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
« First   ‹ Prev
1 2