Trailing Lambda Syntax #9669
-
OverviewIntroduce a syntax in C# that allows developers to pass lambda expressions as the last parameter of a method outside the parentheses, enhancing readability, particularly in cases involving long or multiline lambdas. This feature draws inspiration from Kotlin language syntax. Additionally, for lambdas with exactly one parameter, the implicit variable MotivationIn the current version of C#, lambdas must be written inside the parentheses of method calls: DoSomething(() => {
Console.WriteLine("Hello");
}); For methods where the last parameter is a lambda expression, this syntax can become cumbersome, especially when the lambda is long or multiline. The proposed trailing lambda syntax offers an improved, cleaner way to write such code: DoSomething {
Console.WriteLine("Hello");
}; Syntax
Examples:void Execute(int times, Action action) { ... }
// Current syntax
Execute(3, () => {
Console.WriteLine("Hello");
});
// Proposed trailing lambda syntax
Execute(3) {
Console.WriteLine("Hello");
}; For parameterless methods: // Current
Task.Run(() => {
Console.WriteLine("Done");
});
// Proposed
Task.Run {
Console.WriteLine("Done");
}; For single-parameter lambdas: void Repeat(int times, Action<int> action) { ... }
// Current
Repeat(5, i => Console.WriteLine(i));
// Proposed with trailing lambda
Repeat(5) {
Console.WriteLine(it); // 'it' is implicit
}; Rules
Benefits
Real-World / DSL ScenariosUI Layout DSLThe trailing lambda syntax helps build complex UI hierarchies in a clean and intuitive way, mimicking the structure of a declarative DSL. // Current syntax
Window("Main Window", () => {
Button("Click me", () => {
Console.WriteLine("Button clicked!");
});
TextBox("Enter your name");
Button("Submit", () => {
Console.WriteLine("Submit clicked!");
});
});
// Proposed trailing lambda syntax
Window("Main Window") {
Button("Click me") {
Console.WriteLine("Button clicked!");
}
TextBox("Enter your name")
Button("Submit") {
Console.WriteLine("Submit clicked!");
}
}; Fluent API for ConfigurationWhen defining configuration blocks, the proposed syntax leads to more readable and structured code. // Current syntax
ConfigureServer("localhost", () => {
Port(8080);
UseHttps();
Routes(() => {
Get("/home", () => {
Console.WriteLine("Home page");
});
Post("/login", () => {
Console.WriteLine("Login");
});
});
});
// Proposed trailing lambda syntax
ConfigureServer("localhost") {
Port(8080)
UseHttps()
Routes() {
Get("/home") { Console.WriteLine("Home page"); }
Post("/login") { Console.WriteLine("Login"); }
}
}; LINQComplex LINQ queries become more concise and expressive, eliminating unnecessary lambda parameter declarations. // Current syntax
var adults = people
.Where(person => person.Age >= 18)
.Select(person => person.Name)
.ToList();
// Proposed trailing lambda syntax
var adults = people
.Where { it.Age >= 18 }
.Select { it.Name }
.ToList(); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
See: #1151 |
Beta Was this translation helpful? Give feedback.
See: #1151