digitalmars.D.learn - How to get return type of current method?
- Mike B Johnson (2/2) Apr 18 2017 How can I get the return type of the current method without
- ketmar (2/4) Apr 18 2017 typeof(return)
- Mike B Johnson (2/6) Apr 18 2017 Thanks, sweet and simple!
- Meta (3/12) Apr 18 2017 One note: if the function has a return type of `auto`, you cannot
- =?UTF-8?Q?Ali_=c3=87ehreli?= (29/41) Apr 18 2017 Actually that works but apparently order matters:
How can I get the return type of the current method without specifying the name or any complexity? Similar to typeof(this).
Apr 18 2017
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)
Apr 18 2017
On Tuesday, 18 April 2017 at 23:49:35 UTC, ketmar wrote:Mike B Johnson wrote:Thanks, sweet and simple!How can I get the return type of the current method without specifying the name or any complexity? Similar to typeof(this).typeof(return)
Apr 18 2017
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:One note: if the function has a return type of `auto`, you cannot use `typeof(return)` within the function.Mike B Johnson wrote:Thanks, sweet and simple!How can I get the return type of the current method without specifying the name or any complexity? Similar to typeof(this).typeof(return)
Apr 18 2017
On 04/18/2017 06:40 PM, Meta wrote:On Wednesday, 19 April 2017 at 00:22:14 UTC, Mike B Johnson wrote: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". AliOn Tuesday, 18 April 2017 at 23:49:35 UTC, ketmar wrote:One note: if the function has a return type of `auto`, you cannot use `typeof(return)` within the function.Mike B Johnson wrote:Thanks, sweet and simple!How can I get the return type of the current method without specifying the name or any complexity? Similar to typeof(this).typeof(return)
Apr 18 2017