digitalmars.D.learn - 'is(T==return)' How does is expression with return keyword as
- Timoses (13/13) Jul 05 2018 int fun(T)(T i)
- Alex (5/18) Jul 05 2018 I always thought that "return" is a keyword.
- Timoses (5/24) Jul 05 2018 I think it refers to this section:
- Timoses (25/38) Jul 05 2018 Found it:
- Steven Schveighoffer (10/15) Jul 05 2018 This is not a valid is expression, which is why it's false. IMO, I think...
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?
Jul 05 2018
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... ;)
Jul 05 2018
On Thursday, 5 July 2018 at 11:21:41 UTC, Alex wrote:On Thursday, 5 July 2018 at 10:32:01 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.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... ;)
Jul 05 2018
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; } ´´´
Jul 05 2018
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_expressionshould mention that I mean the 6th paragraph.
Jul 05 2018
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'.
Jul 05 2018
On 7/5/18 6:32 AM, Timoses wrote:int fun(T)(T i) { static assert(is(typeof(return) == T)); //trueThis is the type of the return value for the function you are in.pragma(msg, is(T == return)); // falseThis 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
Jul 05 2018