Skip to content
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

Short syntax for lambda declaration #7463

Closed
Romfos opened this issue Dec 13, 2015 · 6 comments
Closed

Short syntax for lambda declaration #7463

Romfos opened this issue Dec 13, 2015 · 6 comments

Comments

@Romfos
Copy link

Romfos commented Dec 13, 2015

//short lambda declaration for delegates without return keyword
//1. current syntax
example
.ExampleMethodName(() => x + 1)
.ExampleMethodName(() => x + 1).
.ExampleMethodName(() => x + 2).
.ExampleMethodName(() => x + 3).

//short syntax
var x = 0;
example
.ExampleMethodName(=> Console.WriteLine("1"))
.ExampleMethodName(=> x + 1).
.ExampleMethodName(=> x + 2).
.ExampleMethodName(=> x + 3).

//2. short lamda syntax for variable declaration
//current syntax
Action y = () => Console.WriteLine("123");
y();

//short syntax with '=>' operator
Action y => Console.WriteLine("123");
y();

//maybe var y => Console.WriteLine if Console.WriteLine have once signature;

//3. autocast lamda syntax for delegate
//current syntax

Invoke(new Action(() => {..})); or Invoke(delegate () => {..});
//short syntax
Invoke(=> {..});

Where Invoke method have signature > void Invoke(Delegate proc);

@Romfos Romfos changed the title Short syntax for dlambda declaration Short syntax for lambda declaration Dec 13, 2015
@HaloFour
Copy link

Your second case isn't valid C# syntax currently since you are referring to a method group. The valid syntax would be Action y = Console.WriteLine; which is shorter than your proposal.

@Romfos
Copy link
Author

Romfos commented Dec 14, 2015

HaloFour, maybe Action y => Console.WriteLine isn't good example.
Action y => Console.WriteLine("123") //short version
vs
Action y = () => Console.WriteLine("123") //current version
updated

@alrz
Copy link
Contributor

alrz commented Dec 14, 2015

You are suggesting a special assignment operator just for a lambda expression that takes no args? and if I get it right the whole proposal saves two keystrokes per each lambda expression that takes no args? and number 3 is basically #3990.

@Romfos
Copy link
Author

Romfos commented Dec 15, 2015

for improvment by number 3:

//how it's work now:
void Invoke(Delegate proc);
Invoke(new Action(() => {..}));

//not work now
Invoke(() => {...});

//how it's doing work. "() => {...}" automaic casting to Delegate object.
Invoke(() => {..});

//how it's doing work with improvment by number 1
Invoke(=> {...});

Q: You are suggesting a special assignment operator just for a lambda expression that takes no args? and if I get it right the whole proposal saves two keystrokes per each lambda expression that takes no args?

A:

  1. yes, this is improvment for lamda without arguments
  2. This is compile-time feature, not as in Define a default delegate type of Action/Func from method group or lambda #3990.

@alrz
Copy link
Contributor

alrz commented Dec 15, 2015

@Romfos See, there is no "casting" going on, #3990 suggests a "default" delegate type for lambda expressions so it does cover your case. And also, all this happens at compile-time, as it currently does — static type of a lambda expression is known at compile-time.

@gafter
Copy link
Member

gafter commented Mar 20, 2017

We are now taking language feature discussion on https://github.com/dotnet/csharplang for C# specific issues, https://github.com/dotnet/vblang for VB-specific features, and https://github.com/dotnet/csharplang for features that affect both languages.

@gafter gafter closed this as completed Mar 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants