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

[Proposal]: Explicit lambda return type #4934

Open
1 of 4 tasks
jcouv opened this issue Jul 16, 2021 · 11 comments
Open
1 of 4 tasks

[Proposal]: Explicit lambda return type #4934

jcouv opened this issue Jul 16, 2021 · 11 comments
Assignees
Labels
Implemented Needs ECMA Spec This feature has been implemented in C#, but still needs to be merged into the ECMA specification Proposal champion Proposal
Milestone

Comments

@jcouv
Copy link
Member

jcouv commented Jul 16, 2021

  • Proposed
  • Prototype: Not Started
  • Implementation: Not Started
  • Specification: Not Started

Summary

Allow lambdas with explicit return type: int (int x, int y) => x + y.
Allow lambdas with attributes
Infer a natural delegate type for lambdas and method groups

https://github.com/dotnet/csharplang/blob/main/proposals/csharp-10.0/lambda-improvements.md

Design meetings

@julealgon
Copy link

The spec linked has the following example which caught my attention:

var f5 = () : string => null;  // System.Func<string>

Shouldn't this be:

var f5 = string () => null;  // System.Func<string>

?

The document mentions that the explicit return type should come before the arguments, so it feels to me this should still apply for a lambda without arguments (()).

Otherwise, where is the explanation for this : syntax? Is it part of a different proposal? Or is this due to some ambiguity concern with the () operator?

@cston
Copy link
Member

cston commented Jul 27, 2021

Thanks @julealgon. That example was from an earlier iteration of the proposal. I've corrected the example in #4728.

@Thaina
Copy link

Thaina commented Aug 25, 2021

f = static void (_) => { };             // ok

What's the meaning of static here?

And is this feature also include declare method group / natural function as function parameter?

@CyrusNajmabadi
Copy link
Member

What's the meaning of static here?

https://github.com/dotnet/csharplang/blob/main/proposals/csharp-9.0/static-anonymous-functions.md

as function parameter?

This is already possible with Action/Func (and the generic variations).

@Thaina
Copy link

Thaina commented Aug 25, 2021

@CyrusNajmabadi I know, what I mean is not as Action or Func delegate but as a pure method group

@CyrusNajmabadi
Copy link
Member

What is a pure method group?

@Thaina
Copy link

Thaina commented Aug 25, 2021

@CyrusNajmabadi Was it called natural function? There was always something that was exist as an object without proper type in dotnet that was a method that was not converted to Action or Func yet. And will always cause compile time error if we try to assign it to a variable. The error was likely state that it is a method group

My point is it should become first class citizen already instead of require to be converted to Action or Func

@CyrusNajmabadi
Copy link
Member

@CyrusNajmabadi Was it called natural function?

No clue.

There was always something that was exist as an object without proper type in dotnet that was a method that was not converted to Action or Func yet.

Yes. That's what lambdas are. Prior to C# 10 tehy were typeless, and had to be given a type in the code to convert to. Now in C# 10, absent a type in source to convert to, they will naturally convert to Action/Func.

My point is it should become first class citizen

It is. The first class citizen is Action/Func. :)

@333fred 333fred added the Implemented Needs ECMA Spec This feature has been implemented in C#, but still needs to be merged into the ECMA specification label Jan 11, 2022
@ufcpp
Copy link

ufcpp commented May 27, 2022

// OK.
var f = int (string s) => s.Length;

// Error. IIRC, this is rejected intentionally.
dynamic d = int (string s) => s.Length;

// Error. Is this intentional?
var a = new { m = int (string s) => s.Length };

@333fred
Copy link
Member

333fred commented May 27, 2022

@cston?

@cston
Copy link
Member

cston commented May 27, 2022

From lambda-improvements.md:

Function_types are used in a few specific contexts only:

  • implicit and explicit conversions
  • method type inference (§11.6.3) and best common type (§11.6.3.15)
  • var initializers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Implemented Needs ECMA Spec This feature has been implemented in C#, but still needs to be merged into the ECMA specification Proposal champion Proposal
Projects
None yet
Development

No branches or pull requests

8 participants