I suggest we ...

Remove fun keyword from lambda expressions

Maybe make it optional?
Otherwise it is more verbose than C#.

270 votes
Sign in
Sign in with: facebook google
Signed in as (Sign out)
You have left! (?) (thinking…)
Jorge Fioranelli shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →


Sign in
Sign in with: facebook google
Signed in as (Sign out)
  • Alex K commented  ·   ·  Flag as inappropriate

    I'd like to have "fun" optional. Please do it. For those who wants to have it not optional a compiler option which makes it not optional and everyone is happy.

  • Anonymous commented  ·   ·  Flag as inappropriate

    I think the optional use of the fun keyword would be the way to go as well. Just like in React in ES6 the function keyword is no longer necessary

  • Alan Ball commented  ·   ·  Flag as inappropriate

    In response to those who enjoy typing fun, and seeing fun, I do not think the language maintainers would break existing code. The reasonable way to interpret the requirement is to interpret it as optional. I personally think this might be nice. I'm sure there is a clean way to make this work. I would personally prefer the => syntax if possible, simply because many many languages currently use that, including C#.

  • Ideaflare commented  ·   ·  Flag as inappropriate

    I agree with Mastr Mastic, instead of removing it to make it optional.

    Something else that could also be made optional is the let keyword.

  • Will Smith commented  ·   ·  Flag as inappropriate

    I like what we currently have. It isn't that much more verbose than C#. In a way, it seems easier to spot out lambdas in a codebase when you see 'fun'.

  • Maciej J. Bańkowski commented  ·   ·  Flag as inappropriate

    to some Anonymous: variable names are important

    if you are lazy enough to name variables $1 $2 $3 then just name them a b c - it is even shorter.
    Besides, names do matter for maintainability. 2-3 months from now, nobody will know what the original programmer indented to do - names help.

    On topic: It would be so cool to get rid of the 'fun' keyword and make lambdas as sexy as those in C#. However, I doubt it is possible due to statement evaluation logic of F# and ambiguity that would arise without some arbitrary discriminator for lambda expression.

    In C# lambdas are clear from the context and I am not sure F# context is rich enough to get by without the 'fun' keyword but maybe it is - would be awesome.

  • Anonymous commented  ·   ·  Flag as inappropriate

    I think we should preserve the fun keyword to be backward compatible, however
    I really like the Elixir shorthand lambda expressions:

    1..100_000 |> Enum.map(&(&1 * 3)) |> Enum.filter(odd?) |> Enum.sum

    You can for example make
    fun x y z -> x + y + z
    &(&1 + &2 + &3)

    it is not that much shorter, but it frees the programmer from thinking about what to name all those pesky variables.. just like the pipeline frees the programmer from making up a lot of variable names...

  • Andreas Vilinski commented  ·   ·  Flag as inappropriate

    You wanna take the fun out of F#!
    It is not always needed. Often you can write ((+) y) instead of (fun x -> x + y). For other cases I have a R# live template expansion, which just adds me an arrow and braces, defined like here:
    fsfun ==> (fun x -> $END$)
    However it would be nice to map list of pairs like in Scala: List.map (_ + _)

  • Андрей Чебукин commented  ·   ·  Flag as inappropriate

    I agree that it is too long and actually moving to C# syntax would be reasonable.

    However current syntax allows to introduce a snippet for lambda that looks natural
    Type fun, press tab (or just space in case of CodeRush) and fun is almost ready for you.

  • Filip Kopecký commented  ·   ·  Flag as inappropriate

    I think it would be nice if we could write anonymous functions like expressions. The compiler could tell it is a function based on undeclared variables being used.
    (fun x y -> x + y)
    (x + y)

  • Tahir Hassan commented  ·   ·  Flag as inappropriate

    I agree, it looks horrible. C# has a much better lambda syntax, imho.

    Potentially we could use Haskell's backslash:

    [1;2;3] |> Seq.map (\x -> x * x)

    Although it ain't as perfect like C#'s, it would work without making the language ambiguous.

  • Brian commented  ·   ·  Flag as inappropriate

    Seems like it would complicate the language a lot to make it optional. If we were starting from scratch though I'd support it. I would like to hear about what the reasoning behind requiring "fun" for lambda's. Was it simply because it was in OCAML? or was there other technical reasons?

  • Mastr Mastic commented  ·   ·  Flag as inappropriate

    Should be optional, not removed (otherwise code will break).
    And I'm all for it.
    It happens to me countless of times that I forget it because my mind just always expects better.

F# Language

Feedback and Knowledge Base