首页 > 解决方案 > 具有继承的类中的缩进规则?

问题描述

关于缩进规则,我对 F# 编译器(通过 Ionide)有点困惑。

这是我触发警告的代码片段:

type MyGame () as this =
  inherit Game ()
    let graphics = new GraphicsDeviceManager (this)
    let mutable spriteBatch = null
    let mutable state =
      {
        Board = Map.empty
        Selection = List.empty
      }

    do
      this.IsMouseVisible <- true

    // ...

但是,只有当我从前一行中缩进一个或多个字符时,F# 才显得很开心,这看起来……对我来说很奇怪:

type MyGame () as this =
  inherit Game ()
    let graphics = new GraphicsDeviceManager (this)
      let mutable spriteBatch = null
        let mutable state =
          {
            Board = Map.empty
            Selection = List.empty
          }

          do
            this.IsMouseVisible <- true

    // ...

这样的代码应该如何格式化?

标签: f#

解决方案


这是因为你的inherit Game ()行不像接下来的那样缩进。更改缩进,警告消失:

type MyGame () as this =
    inherit Game ()
    let graphics = new GraphicsDeviceManager (this)
    let mutable spriteBatch = null
    let mutable state =
      {
        Board = Map.empty
        Selection = List.empty
      }

    do
      this.IsMouseVisible <- true

    // ...

另请注意,有时您的缩进会优于编译器的建议。


推荐阅读