Search code examples
f#convention

F# convention for brace placement


Is there a generally accepted convention for brace placement in F#? I found some examples in the documentation here, but they didn't seem to be consistent with each other. In particular, and to take an actual example, is there a consensus as to which is better, this

seq {
    for polarity, a in Set.filter (fun (polarity, _) -> polarity) c do
    let c = Set.remove (polarity, a) c
    for a0, a1 in orientations (eqn a) do
    for polarity, b in Set.filter (fun (polarity, _) -> polarity) c do
    let c = Set.remove (polarity, b) c
    for b0, b1 in orientations (eqn b) do
    match unify a0 b0 with
    | None ->
        ()
    | Some m ->
        yield 
            c                                            
            |> Set.add (true, equal (a0, a1))                                        
            |> Set.add (false, equal (a1, b1))                                        
            |> evalClause m }

or this?

seq {
    for polarity, a in Set.filter (fun (polarity, _) -> polarity) c do
    let c = Set.remove (polarity, a) c
    for a0, a1 in orientations (eqn a) do
    for polarity, b in Set.filter (fun (polarity, _) -> polarity) c do
    let c = Set.remove (polarity, b) c
    for b0, b1 in orientations (eqn b) do
    match unify a0 b0 with
    | None ->
        ()
    | Some m ->
        yield 
            c                                            
            |> Set.add (true, equal (a0, a1))                                        
            |> Set.add (false, equal (a1, b1))                                        
            |> evalClause m 
}

And similarly for square brackets in list and array literals that are too large to be written on one line - is it usual to follow the same convention?


Solution

  • It is inherently difficult to assess what constitutes a "generally accepted" convention... Having said that, there are the F# formatting conventions, part of Fantomas, now also integrated in the Visual F# Power Tools. They discuss placement of braces in records and lists. According to that, the closing brace of a record, or closing ] of a list, should be on the list line - as long as the construct enclosed is not too long. For long constructs, as is the case in your example, the brace should be on a new line.

    However, they also say:

    Not everyone likes this style, and variation is ok. For large constructs (> 6 lines) the closing token can be on a fresh line

    There is no explicit mention of seq { ... }, but I feel that the same logic would apply there. Hence, your second example would be the preferred one.

    As for the second part of your question, about arrays and lists: If you go with the Fantomas conventions, short arrays and lists would have their closing thing on the list item, large ones on a new line.