Proposal: Currying support for non Generic, non params, non default functions. #2819
Replies: 10 comments 1 reply
-
I'd rather see: '''' |
Beta Was this translation helpful? Give feedback.
-
I do absolutely love currying, however, I am not totally comfortable with the proposed syntax. |
Beta Was this translation helpful? Give feedback.
-
I'm unclear what advantage, Action<int> curriedPrintNumbers = PrintNumbers(1, 2, 3); brings over the currently supported syntax: Action<int> curriedPrintNumbers = x => Numbers(1, 2, 3, x); that would justify the work required to add this to the language. I'm definitely with @TonyValenti here in that I'd far prefer to be able to express it as: var curriedPrintNumbers = x => Numbers(1, 2, 3, x); and have it infer |
Beta Was this translation helpful? Give feedback.
-
@DavidArno that proposed syntax brings up a lot of covarient/contravarient issues. The proposed syntax is:
|
Beta Was this translation helpful? Give feedback.
-
The variance issues would exist either way as the result of currying would have to be a delegate. For example, the following would not be legal: var f1 = SumNumbers(2);
Func<object, object> f2 = sumNumbers; I'm also with everyone else that if the language were to gain currying that it should do so through an explicit syntax. I'd be cool with something like: var curried = SumNumbers(2, ...); Although that isn't much less verbose than using lambdas today. With overloading and optional parameters it's already quite confusing to the developer to try to figure out exactly what method a given call might try to make, and overload resolution is already one of the most complicated aspects of the C# compiler. If all of a sudden the omission of an argument would result in a successful compilation but return some arbitrary delegate type I think a lot of potential bugs would be missed only to result in weird runtime errors. |
Beta Was this translation helpful? Give feedback.
-
@HaloFour a multitude of e isting languages, including F#, use currying, and this should not result in runtime but compile time errors. Attempting to use a The variance issue I referred to is not in the casting of a Which violates their variance. Because the compiler can't tell the difference between:
And
In the second case, x is...? Attempting to infer the lambda parameters off how they get used violates a lot of stuff. However note that:
Have done away with that I would also be fine with a notation that specifies an intentional curry, to prevent accidental currying (and make intellisense have an easier time) Perhaps the That could open up the option of even doing:
Which would produce an |
Beta Was this translation helpful? Give feedback.
-
Many of those languages, like F#, forbid currying and overloading from being used at the same time. You can curry functions, but not overload them or specify optional parameters. IIRC, you can overload methods, but not curry them.
Depends on what the program does with the result. Yes, if the program tries to treat the result as the return value of the method then it should result in a compiler error. But if the program boxes the type or passes it to a method overloaded by a delegate type the compiler would not catch the problem. Either way the compiler can't check that the invocation has the correct number of arguments.
Oh, you mean inference? Yes, target type inference would probably be a lot more complicated in those scenarios. You can explicitly specify the type of the argument in the lambda, or explicitly specify the type of the target delegate. |
Beta Was this translation helpful? Give feedback.
-
I think I'd be very happy with:
being disambiguated by:
I just hate having to type Action<...> and Func<...> in code. I feel like there should be automatic conversion/equivalency between Actions/Funcs and any kind of delegate with a matching or compatible signature. |
Beta Was this translation helpful? Give feedback.
-
With regard to:
Note that |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Simply put, plain ole functions that dont have optionals, defaults, params, out vars, async, etc on them, should have baked in support for currying. This is a wonderful F# feature I adore and would love to see come to C# as well.
This can already be achieved via lambdas, so the goal would be to simply add support for this with some syntactical sugar to cut down on effort. Especially useful for unit testing where we may have numerous params in a function but we are only changing one of them for each assertion.
Output:
This is extremely useful for unit testing, where perhaps I may have a service with a method like:
And I have a unit test that composes numerous users and calls this method on each of them, my code goes from this:
Instead, I could do for those 5 lines:
Substantially cleaner! I could see some of the more complicated, non plain functions also supporting this. Specifically, async functions probably could work with this. CancellationTokens get along extremely well with currying.
Currently, this is also totally supported in C# anyways, and it purely is syntactical sugar as a shorthand for:
Which would now just become:
So smooth!
Beta Was this translation helpful? Give feedback.
All reactions