Thread overview |
---|
July 05, 2018 'is(T==return)' How does is expression with return keyword as TypeSpecialization | ||||
---|---|---|---|---|
| ||||
int fun(T)(T i) { static assert(is(typeof(return) == T)); //true pragma(msg, is(T == return)); // false static if (is(T ReturnType == return)) pragma(msg, ReturnType); // does not enter return i; } unittest { fun(3); } What's the purpose of 'is(T == return)' if not the above? |
July 05, 2018 Re: 'is(T==return)' How does is expression with return keyword as TypeSpecialization | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timoses | On Thursday, 5 July 2018 at 10:32:01 UTC, Timoses wrote: > int fun(T)(T i) > { > static assert(is(typeof(return) == T)); //true > pragma(msg, is(T == return)); // false > static if (is(T ReturnType == return)) > pragma(msg, ReturnType); // does not enter > return i; > } > unittest > { > fun(3); > } > > What's the purpose of 'is(T == return)' if not the above? I always thought that "return" is a keyword. https://dlang.org/spec/lex.html#keywords And the fact, that you can ask the keyword about its type is just a nice feature... ;) |
July 05, 2018 Re: 'is(T==return)' How does is expression with return keyword as TypeSpecialization | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alex | On Thursday, 5 July 2018 at 11:21:41 UTC, Alex wrote: > On Thursday, 5 July 2018 at 10:32:01 UTC, Timoses wrote: >> int fun(T)(T i) >> { >> static assert(is(typeof(return) == T)); //true >> pragma(msg, is(T == return)); // false >> static if (is(T ReturnType == return)) >> pragma(msg, ReturnType); // does not enter >> return i; >> } >> unittest >> { >> fun(3); >> } >> >> What's the purpose of 'is(T == return)' if not the above? > > I always thought that "return" is a keyword. > > https://dlang.org/spec/lex.html#keywords > > And the fact, that you can ask the keyword about its type is just a nice feature... ;) I think it refers to this section: https://dlang.org/spec/expression.html#is_expression I don't remember where I read this usage (think it was in a book), but I noted it down and now I wonder how it can be used. |
July 05, 2018 Re: 'is(T==return)' How does is expression with return keyword as TypeSpecialization | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timoses | On Thursday, 5 July 2018 at 11:37:16 UTC, Timoses wrote:
>
> I think it refers to this section:
> https://dlang.org/spec/expression.html#is_expression
>
> I don't remember where I read this usage (think it was in a book), but I noted it down and now I wonder how it can be used.
I saw some usage cases, which are like "opposites" to auto fun() declarations:
So either you can declare
´´´
auto fun()
{
int retVal;
...
return retVal;
}
´´´
or
´´´
int fun()
{
typeof(return) retVal;
...
return retVal;
}
´´´
|
July 05, 2018 Re: 'is(T==return)' How does is expression with return keyword as TypeSpecialization | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timoses | On Thursday, 5 July 2018 at 11:37:16 UTC, Timoses wrote:
> I think it refers to this section:
> https://dlang.org/spec/expression.html#is_expression
should mention that I mean the 6th paragraph.
|
July 05, 2018 Re: 'is(T==return)' How does is expression with return keyword as TypeSpecialization | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timoses | On Thursday, 5 July 2018 at 10:32:01 UTC, Timoses wrote: > int fun(T)(T i) > { > static assert(is(typeof(return) == T)); //true > pragma(msg, is(T == return)); // false > static if (is(T ReturnType == return)) > pragma(msg, ReturnType); // does not enter > return i; > } > unittest > { > fun(3); > } > > What's the purpose of 'is(T == return)' if not the above? Found it: http://ddili.org/ders/d.en/is_expr.html Section "is (T identifier == Specifier)" int fun() { return 1337; } template Temp(T) { //pragma(msg, typeof(T)); // is T a function, if so assign its return type to 'retType' static if (is(T retType == return)) { retType Temp(T func) { return func(); } } } void main() { int i = Temp!(typeof(&fun))(&fun); assert(i == 1337); } So, 'return' checks if T is a callable and if so assigns its return type to 'identifier'. |
July 05, 2018 Re: 'is(T==return)' How does is expression with return keyword as TypeSpecialization | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timoses | On 7/5/18 6:32 AM, Timoses wrote: > int fun(T)(T i) > { > static assert(is(typeof(return) == T)); //true This is the type of the return value for the function you are in. > pragma(msg, is(T == return)); // false This is not a valid is expression, which is why it's false. IMO, I think this should be an error. Only is(T == struct), is(T == class), etc. works. > static if (is(T ReturnType == return)) As you have surmised, if T is a function, function pointer, or delegate, then it evaluates to true, and ReturnType is set to the return type of that function. Note, it has nothing to do with the function you are in, unlike typeof(return). -Steve |
Copyright © 1999-2021 by the D Language Foundation