Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which match with indentation rule is at play here?

Tags:

indentation

f#

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?

like image 602
Robert Nielsen Avatar asked Jul 24 '17 03:07

Robert Nielsen


1 Answers

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.

enter image description here

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.

like image 92
Fyodor Soikin Avatar answered Oct 13 '22 14:10

Fyodor Soikin