Why don't Funcs accept more than 16 arguments?
You're hoping for something like variadic type arguments which C# lacks. C# requires the arity of generic types to be fixed, therefore the heinous proliferation of Func
, Action
, and Tuple
types.
If you're language shopping, this feature was added in C++11, but you should probably just use jQuery. :-)
You can just define any delegate you need. So a Func
with 20 parameters would be defined like this:
public delegate R Func<
P0, P1, P2, P3, P4, P5, P6, P7, P8, P9,
P10, P11, P12, P13, P14, P15, P16, P17, P18, P19, R>(
P0 p0, P1 p1, P2 p2, P3 p3, P4 p4,
P5 p5, P6 p6, P7 p7, P8 p8, P9 p9,
P10 p10, P11 p11, P12 p12, P13 p13, P14 p14,
P15 p15, P16 p16, P17 p17, P18 p18, P19 p19);
You could then use it like this:
Func<
int, int, int, int, int, int, int, int, int, int,
int, int, int, int, int, int, int, int, int, int, int> f = (
p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10,
p11, p12, p13, p14, p15, p16, p17, p18, p19) =>
p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9 + p10
+ p11 + p12 + p13 + p14 + p15 + p16 + p17 + p18 + p19;
var r = f(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
C# also lets you use lambda syntax on any delegate, so you could also do this:
public delegate R ArrayFunc<P, R>(params P[] parameters);
And then use it like so:
ArrayFunc<int, int> af = ps => ps.Sum();
var ar = af(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
It's a very flexible and powerful feature of the language.
I think I understand - what you can do with JavaScript and functions (arguments) is preaty neat but it's also not statically typed.
But please note that you never need more than one argument in functional programming anyway. You can chain as much argument as you like by returning another function (this is a common trait in FP and heavaly used with curring a technique also avaiable in JS but only with bending the system a bit).
Of course this is ackward in C#:
Func<A1,Func<A2,Func<A3,...<Func<An,Result>>...>
x1 =>
(x2 =>
(x3 =>
...
(xn =>
{ /*return soomething */ }
))...);
but this is what F# is for ;) and of course you should never make a function with more than a few arguments (way below 16!) anyhow.