Retrieve the return type of the current function

Meta jared771 at gmail.com
Tue May 5 18:23:54 UTC 2020


On Tuesday, 5 May 2020 at 18:19:00 UTC, Meta wrote:
> mixin template magic()
> {
>     alias CallerRet = typeof(return);
>     CallerRet magic()
>     {
>         return CallerRet.init;
>     }
> }

Small edit: you can remove the "CallerRet" alias by doing the 
following:

mixin template magic()
{
     typeof(return) magic()
     {
         return typeof(return).init;
     }
}


Though I wouldn't really recommend it as it's very confusing, 
IMO. This works because "typeof(return)" in the return position 
here refers to the caller's scope, while "typeof(return)" inside 
the function refer's to the function's scope.



More information about the Digitalmars-d-learn mailing list