哪场比赛与缩进规则在这里发挥作用?
Which match with indentation rule is at play here?
这个缩进效果很好:
match 5 with
| k when k < 0 ->
"value is negative"
| k -> "value is non-negative"
|> printfn "%s"
但这不是:
match 5 with
| k when k < 0 ->
"value is negative"
| k ->
"value is non-negative"
|> printfn "%s"
哪个 F# 缩进规则在起作用?
这是 match
下的缩进和运算符的特殊情况的组合。
首先, 在match
下,每个案例的正文都可以从最左边的垂直线开始。例如,这有效:
match 5 with
| x ->
"some value"
其次,对于出现在新行开头的运算符有一个特殊的偏移规则:此类运算符可以位于上一行的左侧,直到宽度运营商加一。例如,这些都以相同的方式工作:
let x =
"abc"
|> printf "%s"
let y =
"abc"
|> printf "%s"
let z =
"abc"
|> printf "%s"
因此,在您的第二个示例中,match
的第二种情况包括 printfn
行,因为正向管道运算符从第一个开始的左侧是在可接受的公差范围内行。
如果将字符串 "value is non-negative"
向右移动两个空格,则前向管道将不再在容差范围内,因此 printfn
行将被解释为在容差范围之外比赛。
match 5 with
| k when k < 0 ->
"value is negative"
| k ->
"value is non-negative"
|> printfn "%s"
在您的第一个示例中,它向右移动了 5 个空格,因此也有效。
这个缩进效果很好:
match 5 with
| k when k < 0 ->
"value is negative"
| k -> "value is non-negative"
|> printfn "%s"
但这不是:
match 5 with
| k when k < 0 ->
"value is negative"
| k ->
"value is non-negative"
|> printfn "%s"
哪个 F# 缩进规则在起作用?
这是 match
下的缩进和运算符的特殊情况的组合。
首先, 在match
下,每个案例的正文都可以从最左边的垂直线开始。例如,这有效:
match 5 with
| x ->
"some value"
其次,对于出现在新行开头的运算符有一个特殊的偏移规则:此类运算符可以位于上一行的左侧,直到宽度运营商加一。例如,这些都以相同的方式工作:
let x =
"abc"
|> printf "%s"
let y =
"abc"
|> printf "%s"
let z =
"abc"
|> printf "%s"
因此,在您的第二个示例中,match
的第二种情况包括 printfn
行,因为正向管道运算符从第一个开始的左侧是在可接受的公差范围内行。
如果将字符串 "value is non-negative"
向右移动两个空格,则前向管道将不再在容差范围内,因此 printfn
行将被解释为在容差范围之外比赛。
match 5 with
| k when k < 0 ->
"value is negative"
| k ->
"value is non-negative"
|> printfn "%s"
在您的第一个示例中,它向右移动了 5 个空格,因此也有效。