New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding the pipeline operator to C# #1309
Comments
We have extension methods for this. |
It's worth noting that while the OCaml style syntax is equally out of place in JavaScript as in C#. The former is slightly more amenable to the pipeline operator because you can trivially define curried functions and also because free functions are more common. |
The extension method is not a perfect match for the pipeline. We need. |
@jeffrey-igaw Why do you think extension methods are no match for pipelines? I think they do fit good in C# syntax as a "substitute". I think we could do most of the things using extensions methods with some |
@MaLiN2223 Hi! I'm not good at English, but i fully agree with issue #74, we quite often use util classes in other library or project, when we want to call the methods with chaining, we write some extension class every each util. and if it is possible. it also has a good effect on local functions, delegates, Entity Something()
{
using(var context = SomeDbContext())
{
// this also change to private static method on this class
// but if need more parameter( four and five more.. ), i like this way.
function Task localFunction(Entity entity) {
// some entity codes...
context.UseThis(entity);
}
// if not pipe operator
// return Updatable.StaticFuncOrAction(localFunction(LibraryUtil.Method(entity.SomeExtendMethod())))
return entity.SomeExtendMethod()
|> LibraryUtil.Method
|> localFunction
|> Updatable.StaticFuncOrAction
}
} In fact, i do not think the pipe operator is a huge deal, but i think it's definitely helpful when we're coding these little pieces. 😄 |
@jeffrey-igaw the pipeline operator wouldn't help with that at all. It doesn't make |
@aluanhaddad Sorry i write wrong return type in this example, actually should return Yes, you're right this is just method cascades with no need additional extension methods(and i don't know FP as well, i don't want C# to be similar to more FP). more simply, Why should we add an extension method for just one call with performance degradation? wouldn't it be better if we could avoid it(even if it is small)? and like tc39 proposal, if the pipeline operator is also able to use await, the code will be cleaner.
or with extensions
Can you explain why you don't need it if possible with examples? |
@jeffrey-igaw I follow you now. Extension methods are statically dispatched so I wouldn't worry about performance but I do agree that it is verbose to define them. Note that if you only have unary functions, you can already write generic extension methods that chain them together. |
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System;
static class Delegates
{
public static Action Static = StaticMethod;
static void StaticMethod() { }
// faster than Static, but bit tricky
public static Action CurriedDelegate = default(object).ExtensionsMethodWithDummyArg;
static void ExtensionsMethodWithDummyArg(this object dummy) { }
// I want this to be curried too, if the pipeline operator is introduced
public static Action CurriedPipeline = default(object) |> StaticWithDummyArg;
static void StaticWithDummyArg(object dummy) { }
}
/// <summary>
/// a result in my PC.
/// Method | Mean | Error | StdDev |
/// ---------------- |---------:|----------:|----------:|
/// Static | 20.37 us | 0.0580 us | 0.0515 us |
/// CurriedDelegate | 15.26 us | 0.0530 us | 0.0496 us | // faster
/// </summary>
public class InvokeBenchmark
{
const int N = 10000;
[Benchmark] public void Static() => Invoke(Delegates.Static);
[Benchmark] public void CurriedDelegate() => Invoke(Delegates.CurriedDelegate);
private static void Invoke(Action action)
{
for (int i = 0; i < N; i++) action();
}
}
class Program
{
static void Main() => BenchmarkRunner.Run<InvokeBenchmark>();
} |
Has there been any significant deliberation on this suggestion as time has gone on? I adore the pipe operator in Elixir and would love to have it in C# as well. |
@samuelludwig No. If/when such things happen, the issues are updated accordingly. Thanks! :) |
extension method can't handle this case:
|
Extension methods can only be used when the method itself being declared as an extension method. However, pipeline operator allows non-extension methods to be piped. |
With Extension methods you are forced to write additional classes methods and so forth |
Sad this is closed 😭 |
@winstonpuckett it's closed because it's a duplicate of two existing issues, not because the idea has been rejected. |
Oh, lol. Thank you. This was my first time responding to a github issue. Now I know to check the bottom for the comment on why it was closed. |
The Pipeline operator is going to add to Javascript, and I see it greatly enhances readability to multiple method invocation https://github.com/tc39/proposal-pipeline-operator
How abount adding it to C#?
The text was updated successfully, but these errors were encountered: