Indentation change after if-else expression not taken into account?

后端 未结 1 1624
梦如初夏
梦如初夏 2021-01-06 04:54

Given this this operator for evaluation of side effects in pipelines

let inline (|>!) a f = f a ; a 

and this code snippet

         


        
相关标签:
1条回答
  • 2021-01-06 05:21

    This is not a bug, and it doesn't happen specifically for operators longer than two characters. This is a funny consequence of F#'s permissive offset rules.

    When aligning lines of the same nestedness level, they have to be at the same indentation, like this:

    let foo =
         bar
         baz
         qux
    

    But this is not allowed:

    let foo =
         bar
          baz  // Indented too much to the left
         qux
    

    And neither is this:

    let foo =
         bar
        baz  // Indented too little
         qux
    

    When dealing with constructs that create nested blocks, such as if/then, this rule is used to determine when the block is over: it's when the indentation alignment is broken.

    let x =
         if 1 = 1 then
           bar
           baz
         qux
    

    But there is an exception to this rule: if the line starts with an operator, then it is allowed to be shifted to the left by at most the operator size plus 1 characters, and it will still be considered to be at "current" indentation.

    For example, this works:

    let x =
           1
         + 2
         + 3
    

    But when you have operators of different sizes, it gets tricky. This works:

    let x =
           1
         + 2
         + 3
        <> 6
    

    But this doesn't:

    let x =
            1
         +  2
         +  3
         <> 6
    

    0 讨论(0)
提交回复
热议问题