From 34e998889ab0da252087ea26ea46d3d52a485b3b Mon Sep 17 00:00:00 2001 From: Guillaume Chatelet Date: Fri, 22 Jun 2012 23:26:05 +0200 Subject: [PATCH] Adding arity traits --- std/traits.d | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/std/traits.d b/std/traits.d index e840da9ce..8664efa3f 100644 --- a/std/traits.d +++ b/std/traits.d @@ -364,6 +364,29 @@ unittest static assert(is(P_dglit[0] == int)); } +/** +Returns the number of arguments of function $(D func) + +Example: +--- +void foo(){} +static assert(arity!foo==0); +void bar(uint){} +static assert(arity!bar==1); +--- + */ +template arity(alias func) + if (isCallable!func) +{ + enum uint arity = (ParameterTypeTuple!func).length; +} + +unittest { + void foo(){} + static assert(arity!foo==0); + void bar(uint){} + static assert(arity!bar==1); +} /** Returns a tuple consisting of the storage classes of the parameters of a