Add extension delegate #4944
-
SummaryAdd the ability to create an extension delegate. Example of creation: public static class ObjectExtensions
{
public static T Apply<T>(this T self, Action<this T> block)
{
block(self);
return self;
}
} Example of use with Linq: var users = new List<User> { new("Igor", 34), new("Lera", 23), new("Max", 54)};
var benefitUsers = users.Where(this => Age < 35).ToList();
// two variant
var benefitUsers = users.Where(() => Age < 35).ToList();
record User(string Name, int Age); MotivationExtension methods of this kind are often used (especially in Linq): public static T Also<T>(this T self, Action<T> block)
{
block(self);
return self;
} Because of this, you often have to write the same name for the delegate parameter. An example of using the ReactiveUI binding without adding this in C#: this.WhenActivated(disposable =>
{
this.Bind(ViewModel, x => x.TheText, x => x.TheTextBox.Text)
.DisposeWith(disposable);
this.OneWayBind(ViewModel, x => x.TheText, x => x.TheTextBlock.Text)
.DisposeWith(disposable);
this.BindCommand(ViewModel, x => x.TheTextCommand, x => x.TheTextButton)
.DisposeWith(disposable);
}); With the addition of this: this.WhenActivated(disposable =>
{
this.Bind(ViewModel, vm => vm.TheText, this => TheTextBox.Text)
.DisposeWith(disposable);
this.OneWayBind(ViewModel, vm => vm.TheText, this => TheTextBlock.Text)
.DisposeWith(disposable);
this.BindCommand(ViewModel, vm => vm.TheTextCommand, this => TheTextButton)
.DisposeWith(disposable);
}); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
There have been proposals for achieving the same goal using different syntax in the past: #91, #1151, #602. Specifically for this syntax, what confuses me is that your own example shows that this can make the code longer ( |
Beta Was this translation helpful? Give feedback.
There have been proposals for achieving the same goal using different syntax in the past: #91, #1151, #602.
Specifically for this syntax, what confuses me is that your own example shows that this can make the code longer (
this => TheText
is one more character thanx => x.TheText
). So why would this be a worthwhile addition to the language?