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

non symmetricity between fun and function for tuple forces usage of parens #1336

Open
5 of 6 tasks
smoothdeveloper opened this issue Nov 14, 2023 · 7 comments
Open
5 of 6 tasks

Comments

@smoothdeveloper
Copy link
Contributor

those are allegedly similar semantically:

fun a -> //...
function a -> //...

but for tuple, it doesn't work

fun a,b -> // error FS0010: Unexpected symbol ',' in lambda expression. Expected '->' or other token.
function a,b -> //...

I wonder if this can be fixed without ambiguity, because it would be an incomplete lambda in current version of the language and it would reestablish fuller symmetry with function construct.

The existing way of approaching this problem in F# is to use function or wrap with parens.

Maybe a code fix for putting the parens, or turning into function would make sense if we can't make this suggestion work / it is rejected on some grounds.

Pros and Cons

The advantages of making this adjustment to F# are semantically more consistent.

The disadvantages of making this adjustment to F# are may prevent other constructs in the future.

Extra information

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

Related suggestions: (put links to related suggestions here)

Affidavit

Please tick these items by placing a cross in the box:

  • This is not a question (e.g. like one you might ask on StackOverflow) and I have searched StackOverflow for discussions of this issue
  • This is a language change and not purely a tooling change.
  • This is not something which has obviously "already been decided" in previous versions of F#. I
  • I have searched both open and closed suggestions on this site and believe this is not a duplicate

Please tick all that apply:

  • This is not a breaking change to the F# language design
  • I or my company would be willing to help implement and/or test this

For Readers

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

@Martin521
Copy link

Martin521 commented Nov 14, 2023

fun and function are actually different.
fun expects a parameter list and a function body (where each parameter must be enclosed in parentheses if it is a pattern or has a type annotation), in the same way as a named function.
function expects match expressions, separated by '|'.
So the mentioned behavior makes sense.

@Martin521
Copy link

Actually, I think the use of function should be discouraged in favour of proper matches, except for one-liners.
But for function one-liners you get a no-no from Fantomas :-(

@realparadyne
Copy link

No-no from me on discouraging the use of function. They are brilliant for multiple step transformations starting with a regular match to transform something and then doing another match on that result without having to think up a name to bind the intermediate result(s) to.

Basically, anywhere that an expression result needs to be the 'function' of some singular input value that can have multiple shapes. Without having to come up with a name and use it twice, redundantly, both times.

@realparadyne
Copy link

No-no from me on discouraging the use of function. They are brilliant for multiple step transformations starting with a regular match to transform something and then doing another match on that output without having to think up a name to bind the intermediate result(s) to.

Basically anywhere that an expression result needs to be the 'function' of some input value that can have multiple shapes.

@konst-sh
Copy link

to me also the tuple between fun and -> looks unambiguous enough to interpret it as function argument pattern,
but it may cause problems if fun will become optional #168

@LyndonGingerich
Copy link

those are allegedly similar semantically:

fun a -> //...
function a -> //...

Could you explain? To the best of my knowledge, the second line is invalid.

@smoothdeveloper
Copy link
Contributor Author

@LyndonGingerich in such case:

let data = [1,"1";2,"2"]
data |> List.map (function a,b -> a * 2)
data |> List.map (fun (a,b) -> a * 2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants