Skip to content

Commit

Permalink
Merge pull request #1141 from monarchdodra/Fun
Browse files Browse the repository at this point in the history
Improve XnaryFun argument passing
  • Loading branch information
DmitryOlshansky committed Jan 25, 2014
2 parents bcf2be7 + 6ceda93 commit 19b4deb
Showing 1 changed file with 15 additions and 19 deletions.
34 changes: 15 additions & 19 deletions std/functional.d
Expand Up @@ -36,25 +36,14 @@ assert(isEven(2) && !isEven(1));
----
*/

template unaryFun(alias fun, bool byRef = false, string parmName = "a")
template unaryFun(alias fun, string parmName = "a")
{
static if (is(typeof(fun) : string))
{
static if (byRef)
auto unaryFun(ElementType)(auto ref ElementType __a)
{
auto unaryFun(ElementType)(ref ElementType __a)
{
mixin("alias __a "~parmName~";");
mixin("return (" ~ fun ~ ");");
}
}
else
{
auto unaryFun(ElementType)(ElementType __a)
{
mixin("alias __a "~parmName~";");
mixin("return (" ~ fun ~ ");");
}
mixin("alias " ~ parmName ~ " = __a ;");
return mixin(fun);
}
}
else
Expand All @@ -63,6 +52,13 @@ template unaryFun(alias fun, bool byRef = false, string parmName = "a")
}
}

/+ Undocumented, will be removed December 2014+/
deprecated("Parameter byRef is obsolete. Please call unaryFun!(fun, parmName) directly.")
template unaryFun(alias fun, bool byRef, string parmName = "a")
{
alias unaryFun = unaryFun!(fun, parmName);
}

unittest
{
static int f1(int a) { return a + 1; }
Expand Down Expand Up @@ -99,11 +95,11 @@ template binaryFun(alias fun, string parm1Name = "a",
static if (is(typeof(fun) : string))
{
auto binaryFun(ElementType1, ElementType2)
(ElementType1 __a, ElementType2 __b)
(auto ref ElementType1 __a, auto ref ElementType2 __b)
{
mixin("alias __a "~parm1Name~";");
mixin("alias __b "~parm2Name~";");
mixin("return (" ~ fun ~ ");");
mixin("alias "~parm1Name~" = __a ;");
mixin("alias "~parm2Name~" = __b ;");
return mixin(fun);
}
}
else
Expand Down

0 comments on commit 19b4deb

Please sign in to comment.