csharplang icon indicating copy to clipboard operation
csharplang copied to clipboard

[Proposal]: Explicit lambda return type

Open jcouv opened this issue 3 years ago • 11 comments

  • [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

jcouv avatar Jul 16 '21 18:07 jcouv

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?

julealgon avatar Jul 27 '21 20:07 julealgon

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.

cston avatar Jul 27 '21 20:07 cston

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?

Thaina avatar Aug 25 '21 04:08 Thaina

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 avatar Aug 25 '21 05:08 CyrusNajmabadi

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

Thaina avatar Aug 25 '21 05:08 Thaina

What is a pure method group?

CyrusNajmabadi avatar Aug 25 '21 05:08 CyrusNajmabadi

@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

Thaina avatar Aug 25 '21 05:08 Thaina

@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. :)

CyrusNajmabadi avatar Aug 25 '21 05:08 CyrusNajmabadi

// 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 };

ufcpp avatar May 27 '22 04:05 ufcpp

@cston?

333fred avatar May 27 '22 04:05 333fred

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

cston avatar May 27 '22 14:05 cston