在F#中格式化警告

Nic*_*ner 2 formatting f#

以下代码会在每个代码上触发格式化警告let("可能的错误缩进"):

module UtilTests =
    [<Test>] let simpleWithNth ()= true |> should be True
    [<Test>] let negIndex () = true |> should be True
    [<Test>] let tooBigIndex () = true |> should be True
    [<Test>] let lastIndex () = true |> should be True
Run Code Online (Sandbox Code Playgroud)

以下不是:

module UtilTests =
    [<Test>] let simpleWithNth ()= true |> should be True
     [<Test>] let negIndex () = true |> should be True
      [<Test>] let tooBigIndex () = true |> should be True
       [<Test>] let lastIndex () = true |> should be True
Run Code Online (Sandbox Code Playgroud)

为什么它们希望每个let缩进比上面的缩进更多?(有没有办法让Visual Studio 2012自动格式化?)

Tom*_*cek 5

正如Brian在评论中所说,将属性应用于let函数的常用方法是在let绑定之前在行上写入属性.我也希望你编写的代码可以工作,因为函数的主体在同一行,但显然编译器不这么认为....

但是,还有另一种方法可以将属性应用于let在您的示例中运行良好的函数:

module UtilTests = 
  let [<Test>] simpleWithNth ()= true |> should be True 
  let [<Test>] negIndex () = true |> should be True 
  let [<Test>] tooBigIndex () = true |> should be True 
  let [<Test>] lastIndex () = true |> should be True 
Run Code Online (Sandbox Code Playgroud)

如果您正在编写递归函数,则需要此样式 - 然后前一行的属性不起作用,您需要编写let rec [<Foo>] foo () = ... and [<Bar>] bar () = ....