Support native partial application for methods and delegates #3937
-
When digging into functional programming we usually goes through partial application (of methods), and since C# is getting more functional each time, would be nice have a suggar for it become more usual. Partial application is the process of pass part of the arguments of a method, returning new a method that receives the rest of the arguments. For example, If you provide 1 out of 3 arguments, it'll return a function that takes 2 arguments. If you provide 2 out of 3 arguments, it'll return a method that takes 1 argument. The general benefit that this approach gives you is that it’s a way to create specialized methods from more general methods. It is a simple technique, but very powerfull. The idea of this proposal is allow it for methods (with return value or not) and delegates (like Func, Action, etc) through syntax suggar of the current approach, that is to wrap the original call by writting an anonymous function. There are possible complications in cases bellow, so this feature would not be avaible for them in this proposal:
Consider method public int Add(int num1, int num2, int num3) => num1 + num2 + num3; Current approach: Func<int, int, int> add4 = (num2, num3) => Add(4, num2, num3);
Func<int, int> add5 = num3 => Add(2, 3, num3); Could be simplified having the suggar: Func<int, int, int> add4 = Add(4);
Func<int, int> add5 = Add(2, 3); We could also use named arguments to specify non sequential parameters Current approach: Func<int, int> add4 = num2 => Add(3, num2, 1); Could be simplified having the suggar: Func<int, int> add4 = Add(num1: 3, num3: 1); Something that could simplify even more these declarations is that, since partial application would be avaible only for methods/delegates and not for linq's expressions, partial application could be inferred as Func<int, int> add5 = Add(2, 3);
Func<int, int> add4 = Add(num1: 3, num3: 1); Could be simplified even more to var add5 = Add(2, 3);
var add4 = Add(num1: 3, num3: 1); |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 4 replies
-
IMO the explicit syntax is much clearer about what's going on. There's nothing about |
Beta Was this translation helpful? Give feedback.
-
Doesn't work when method arguments have default values either: int PerformCalculation(int x, int y = 0)
{
// something...
} Then: var result = PerformCalculation(100); Would this be a partially applied call? Or a regular method call? I'm a fan of FP too. But some ideas just don't fit in C#. |
Beta Was this translation helpful? Give feedback.
-
Would using discards as placeholders work? e.g. var add5 = Add(2, 3, _); This way you're explicitly specifying that you're only partially applying some parameters, and are explicitly ignoring the rest. It could probably be implemented similarly to I've personally been interested in using partial application myself, to hide implementation details where appropriate, and while I'm not sure a dedicated syntax is super necessary for my use cases, it would be a lot more convenient to write |
Beta Was this translation helpful? Give feedback.
-
I gonna close this discussion. |
Beta Was this translation helpful? Give feedback.
IMO the explicit syntax is much clearer about what's going on. There's nothing about
Add(4)
that tells me that this is some kind of partial application resulting in the allocation of some arbitrary type of delegate. Having the compiler try to do that automatically based on incomplete method invocations would complicate method overload resolution considerably.