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 by fvbommel — 2009-04-22T07:56:04Z
*** Bug 2877 has been marked as a duplicate of this bug. ***
Comment #2 by fvbommel — 2009-04-22T07:57:16Z
Isn't this already implemented (in D2)?
Comment #3 by braddr — 2009-06-08T01:08:39Z
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 by witold.baryluk+d — 2009-12-26T18:52:06Z
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.