csharplang
csharplang copied to clipboard
[Proposal]: Explicit lambda return type
- [x] 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
- https://github.com/dotnet/csharplang/blob/main/meetings/2021/LDM-2021-03-03.md
- https://github.com/dotnet/csharplang/blob/main/meetings/2021/LDM-2021-04-12.md#lambda-improvements
- https://github.com/dotnet/csharplang/blob/main/meetings/2021/LDM-2021-04-21.md#inferred-types-for-lambdas-and-method-groups
- https://github.com/dotnet/csharplang/blob/main/meetings/2021/LDM-2021-05-10.md
- https://github.com/dotnet/csharplang/blob/main/meetings/2021/LDM-2021-06-02.md#lambda-return-type-parsing
- https://github.com/dotnet/csharplang/blob/main/meetings/2021/LDM-2021-06-21.md#open-questions-for-lambda-return-types (inference,
ref
return syntax) - https://github.com/dotnet/csharplang/blob/main/meetings/2021/LDM-2021-07-12.md
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?
Thanks @julealgon. That example was from an earlier iteration of the proposal. I've corrected the example in https://github.com/dotnet/csharplang/pull/4728.
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?
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).
@CyrusNajmabadi I know, what I mean is not as Action
or Func
delegate but as a pure method group
What is a pure method group?
@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 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. :)
// 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 };
@cston?
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