'is(T==return)' How does is expression with return keyword as TypeSpecialization

Timoses timosesu at gmail.com
Thu Jul 5 12:42:06 UTC 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'.


More information about the Digitalmars-d-learn mailing list