This indentation works fine:
match 5 with
| k when k < 0 ->
"value is negative"
| k -> "value is non-negative"
|> printfn "%s"
but this does not:
match 5 with
| k when k < 0 ->
"value is negative"
| k ->
"value is non-negative"
|> printfn "%s"
Which F# indentation rule is at play?
This is a combination of indentation under match
and special case for operators.
First, under match
, the body of each case can start as far left as the vertical line. For example, this works:
match 5 with
| x ->
"some value"
Second, there is a special offset rule for operators that appear at the beginning of a new line: such operators can be to the left of the previous line up to the width of the operator plus one. For example, these all work identically:
let x =
"abc"
|> printf "%s"
let y =
"abc"
|> printf "%s"
let z =
"abc"
|> printf "%s"
So, in your second example, the second case of the match
includes the printfn
line, because the forward pipe operator is within the acceptable tolerance to the left from the beginning of the first line.
If you move the string "value is non-negative"
just two spaces to the right, the forward pipe won't be within the tolerance anymore, and so the printfn
line will be interpreted as being outside the match.
match 5 with
| k when k < 0 ->
"value is negative"
| k ->
"value is non-negative"
|> printfn "%s"
In your first example, it is moved 5 spaces to the right, so that works too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With