D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 2876 - Enhancement to 'auto' return
Summary: Enhancement to 'auto' return
Status: RESOLVED WORKSFORME
Alias: None
Product: D
Classification: Unclassified
Component: dlang.org (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords:
: 2877 (view as issue list)
Depends on:
Blocks:
 
Reported: 2009-04-22 07:48 UTC by galaxylang
Modified: 2015-06-09 05:13 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description galaxylang 2009-04-22 07:48:41 UTC
I'm interest in D's metaprogram,surely D is the most powerful language now.
However when i write D program,i found if we can program by this way:

auto ReturnStatic(int i)(int a)
{
   int f1(int b){return a+b;}
   double f2(double c){return c+a;}
   static if(i==0){return &f1;}
   else return &f2;
}

this will be great enhance the language power
Comment 1 Frits van Bommel 2009-04-22 07:56:04 UTC
*** Bug 2877 has been marked as a duplicate of this bug. ***
Comment 2 Frits van Bommel 2009-04-22 07:57:16 UTC
Isn't this already implemented (in D2)?
Comment 3 Brad Roberts 2009-06-08 01:08:39 UTC
I doubt the current auto return deduction could handle this code, but I haven't tried it.

Changing it to an enhancement request for a future version.
Comment 4 Witold Baryluk 2009-12-26 18:52:06 UTC
I don't see any problem with this example. It is quite simple to compile, and in fact it works in DMD 2.037 as desired. This example uses three things which are fully supported:
 - static if over template parameters, no problem for long time
 - coping/allocating variable on heap if they are used in delegate (or nested functions) and returned from function (escaping scope), no problem in most cases
 - infering return type, no problem as we have exactly one return (after static if selection), and its type is quite "simple"  (it is delegate int(int), or delegate float(float)), and compiler knows this.


Please close this bug with WORKSFORME.