Thread overview
How to get return type of current method?
4 days ago
Mike B Johnson
4 days ago
ketmar
4 days ago
Mike B Johnson
4 days ago
Meta
4 days ago
Ali Çehreli
4 days ago
How can I get the return type of the current method without specifying the name or any complexity? Similar to typeof(this).
4 days ago
Mike B Johnson wrote:

> How can I get the return type of the current method without specifying the name or any complexity? Similar to typeof(this).

typeof(return)
4 days ago
On Tuesday, 18 April 2017 at 23:49:35 UTC, ketmar wrote:
> Mike B Johnson wrote:
>
>> How can I get the return type of the current method without specifying the name or any complexity? Similar to typeof(this).
>
> typeof(return)

Thanks, sweet and simple!
4 days ago
On Wednesday, 19 April 2017 at 00:22:14 UTC, Mike B Johnson wrote:
> On Tuesday, 18 April 2017 at 23:49:35 UTC, ketmar wrote:
>> Mike B Johnson wrote:
>>
>>> How can I get the return type of the current method without specifying the name or any complexity? Similar to typeof(this).
>>
>> typeof(return)
>
> Thanks, sweet and simple!

One note: if the function has a return type of `auto`, you cannot use `typeof(return)` within the function.
4 days ago
On 04/18/2017 06:40 PM, Meta wrote:
> On Wednesday, 19 April 2017 at 00:22:14 UTC, Mike B Johnson wrote:
>> On Tuesday, 18 April 2017 at 23:49:35 UTC, ketmar wrote:
>>> Mike B Johnson wrote:
>>>
>>>> How can I get the return type of the current method without
>>>> specifying the name or any complexity? Similar to typeof(this).
>>>
>>> typeof(return)
>>
>> Thanks, sweet and simple!
>
> One note: if the function has a return type of `auto`, you cannot use
> `typeof(return)` within the function.

Actually that works but apparently order matters:

import std.stdio;

bool condition;

auto foo() {
    // Compilation ERROR here
    writeln(typeof(return).stringof);

    if (condition) {
        return 1.5;
    }

    return 42;
}

void main() {
    foo();
}

Error: cannot use typeof(return) inside function foo with inferred return type

But if you move typeof(return) after the first return statement, which determines the return type of the function per spec, then it works:

auto foo() {
    if (condition) {
        return 1.5;
    }

    // Works here
    writeln(typeof(return).stringof);

    return 42;
}

Prints "double".

Ali