Search code examples
f#visual-studio-codeionide

F# : wrap tuples into multiple lines


Let's say I have a simple Tree :

type Tree =
    | BinaryNode of Tree * int * Tree
    | Tip 

    member this.Sum =
        match this with
        | Tip -> 0
        | BinaryNode(left, value, right) -> left.Sum + value + right.Sum

and I define a tree by :

let tree = BinaryNode( BinaryNode( Tip, 2, Tip ), 1, BinaryNode( Tip, 3, Tip ))

But I think it's really hard to read if we have a tree with multiple nodes. So I wrap it into multiple lines as below :

[<EntryPoint>]
let main argv =

    let tree = 
        BinaryNode( 
            BinaryNode( Tip, 2, Tip ), 
            1, 
            BinaryNode( Tip, 3, Tip )
        )

    printf "%A" (tree.Sum)

    0

The above code works as expected in Visual Studio. But when I use Visual Studio Code with Ionide , it reminds me that :

Lint: Comma in tuple instantiation should be followed by single space.

enter image description here

My question :

  1. I'm confused. Is it bad to wrap tuples into multiple lines ?
  2. If not, how to make the lint shut up ?
  3. If the answer is Yes, how to improve the readability when we have ten nodes ?

Solution

  • It don't think it's necessarily bad to do. Well, I hope not, since I do it all the time. Especially in cases like this, where the line would otherwise be much too wide, to be pleasent to read.

    As far as I can tell, Ionide uses FSharpLint. According to the docs, individual warnings can be disabled on a project level, by placing an XML file called Settings.FSharpLint in the project folder, with the desired configuration.

    I think, in your case, the file should have the following content:

    <?xml version="1.0" encoding="utf-8"?>
    <FSharpLintSettings>
      <Analysers>
        <TupleCommaSpacing>
          <Enabled>false</Enabled>
        </TupleCommaSpacing>
      </Analysers>
    </FSharpLintSettings>