fsharp / fslang-suggestions

The place to make suggestions, discuss and vote on F# language and core library features
345 stars 21 forks source link

Allow specifying lambdas return type #1297

Closed lucasteles closed 9 months ago

lucasteles commented 1 year ago

Allowing specifying the explicit return type of lambdas is something that can't be done and could be useful in some cases and it is also something that should be supported to be paired with let functions.

One case is dealing with implicit return types

open Microsoft.AspNetCore.Http
open Microsoft.AspNetCore.Http.HttpResults
open type TypedResults

let handler n : Results<Ok, BadRequest> = // ok
    if n % 2 = 0 then Ok() else BadRequest()

let handler2: int -> Results<Ok, BadRequest> = // ok 
    fun n -> if n % 2 = 0 then Ok() else BadRequest()

let handler3 = // do not compile
    fun (n: int) : Results<Ok, BadRequest> -> if n % 2 = 0 then Ok() else BadRequest()

Pros and Cons

The advantages of making this adjustment to F# are:

The disadvantages of making this adjustment to F# are ...

Extra information

Estimated cost (XS, S, M, L, XL, XXL): ?

Related suggestions: (put links to related suggestions here)

Affidavit (please submit!)

Please tick this by placing a cross in the box:

Please tick all that apply:

For Readers

If you would like to see this issue implemented, please click the :+1: emoji on this issue. These counts are used to generally order the suggestions by engagement.

vzarytovskii commented 1 year ago

You can specify the type of the expression right now, if needed:

let handler =
    fun (n: int) -> (if n % 2 = 0 then Ok() else Error(n)) : obj
cartermp commented 1 year ago

Gonna tag this as probably not -- while in theory it could be nice to have the return type definition closer to the parameters, this doesn't seem worth doing:

vzarytovskii commented 9 months ago

Closing down all probably not issues, until there's another consideration about it.