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

Mutable expressions should be on one line #2280

Closed
Thorium opened this issue May 25, 2022 · 4 comments
Closed

Mutable expressions should be on one line #2280

Thorium opened this issue May 25, 2022 · 4 comments

Comments

@Thorium
Copy link
Member

Thorium commented May 25, 2022

F# Style-guide says:

Formatting mutation expressions

Mutation expressions location <- expr are normally formatted on one line.

So this is the correct style:

let test x =
   if x.IsNone then 
       x.Value <- "a"
   ()

The mutation is so untypical and notable action, that it should not be hidden, it should be always bring into attention of the person reading the code. But Fantomas dumps the <- arrow some times to same line as pre-conditions, e.g.:

let test x =
    if x.IsNone then x.Value <- "a"
    ()
@auduchinok
Copy link
Contributor

auduchinok commented May 25, 2022

It may depend on particular code example and context, and it's possible to imagine cases where a user would want to have this written as single-line and as multiline.

let f x =
    if condition1 x then x <- expr1
    if condition2 x then x <- expr2
    if condition3 x then x <- expr3

    x
let f x =
    expr1 arg1 arg2

    if condition then
        x <- expr2

    expr arg3 arg4

What we do in R# formatter for C# and other languages is we try to keep original code unless it violates the chosen formatter settings, because in many places these small stylistic choices are allowed by the style guide and are what makes the code easier to read or helps to convey the meaning by highlighting important or hiding unimportant parts.

@Thorium
Copy link
Member Author

Thorium commented May 25, 2022

R# formatter approach sounds nice, but I guess would need some re-design from Fantomas side, basically to carry the full original source code as trivia? Or do you have some data-structure of "allowed flexibility"?

@nojaf
Copy link
Contributor

nojaf commented May 26, 2022

Hello, the style guide does not mention anything about how the mutable expression should react relative to its placement. The line Mutation expressions location <- expr are normally formatted on one line is about the expression itself and not with respect to its location.
The examples on https://docs.microsoft.com/en-us/dotnet/fsharp/style-guide/formatting#formatting-mutation-expressions do not support your claim.

Stylistic bugs in Fantomas need to be reported in the online tool as well, accompanied with the actual link to the section of the style guide which is not respected.

@nojaf nojaf closed this as completed May 26, 2022
@dsyme
Copy link
Contributor

dsyme commented May 26, 2022

@Thorium See decision here: fsharp/fslang-design#663 (comment)

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

4 participants