大网数据广告活动
.NET 与 C# 开发

F# 代码格式设置准则

· 52CE 我爱测

了解有关格式设置准则F#代码。

本文提供有关如何设置代码的格式的指导原则,以便在F#代码是:

  • 通常以更清晰的形式查看
  • 符合应用的 Visual Studio 中的工具和其他编辑器格式设置约定
  • 类似于其他代码联机

这些指导基于的全面指南F#格式设置约定通过Anh Dung Phan。

缩进的一般规则

F#默认情况下使用有意义的空白。 以下指南旨在提供指导如何能够同时处理这可以施加一些挑战。

使用的空间

需要缩进时,您必须使用空格,不是制表符。 至少一个空间是必需的。 你的组织可以创建以指定要用于缩进; 的空格数的编码标准典型的缩进发生每个级别的缩进的两个、 三个或四个空格。

我们建议每个缩进的 4 个空格。

也就是说,缩进的程序是一个主观问题。 变体是好的但应遵循的第一个规则缩进的一致性。 选择一种普遍接受的缩进样式,并在整个代码库系统地使用它。

格式设置的空白区域

F#是敏感的空白区域。 尽管正确缩进涵盖空白的大多数语义,有一些需要考虑的其他事项。

算术表达式中的格式设置运算符

始终使用二进制算术表达式周围的空白区域:

let subtractThenAdd x = x - 1 + 3

一元-运算符应始终具有它们传递的值在后面紧跟:

// OK
let negate x = -x

// Bad
let negateBad x = - x

添加空白字符后的-运算符可能会导致其他人混淆。

总之,务必始终:

  • 含空白区域的外侧代码二元运算符
  • 一元运算符后没有尾随空格

二进制算术运算符准则是尤为重要。 失败来包围二进制-运算符,与特定格式设置选项结合使用时可能会导致它解释为一元-。

外侧代码具有空白的自定义运算符的定义

始终使用空格来包围运算符定义:

// OK
let ( !> ) x f = f x

// Bad
let (!>) x f = f x

对于任何自定义的运算符开头*和具有多个字符,您需要添加到的要避免编译器不明确的定义开头的空白区域。 因此,我们建议你只需括起来的单个空白字符的所有运算符的定义。

环绕含空白区域的函数参数箭头

在定义函数的签名时,使用周围的空白区域->符号:

// OK
type MyFun = int -> int -> string

// Bad
type MyFunBad = int->int->string

含空白区域的外侧代码函数自变量

在定义函数时,使用每个自变量周围的空白区域。

// OK
let myFun (a: decimal) b c = a + b + c

// Bad
let myFunBad (a:decimal)(b)c = a + b + c

类型批注

右填充函数参数类型批注

在定义的参数和类型批注时,使用之后的空白区域:符号:

// OK
let complexFunction (a: int) (b: int) c = a + b + c

// Bad
let complexFunctionBad (a :int) (b :int) (c:int) = a + b + c
含空白区域的外侧代码返回类型批注

中的 let 绑定函数或值类型批注 (在函数的情况下返回类型),使用空白区域之前和之后:符号:

// OK
let expensiveToCompute : int = 0 // Type annotation for let-bound value
let myFun (a: decimal) b c : decimal = a + b + c // Type annotation for the return type of a function
// Bad
let expensiveToComputeBad1:int = 1
let expensiveToComputeBad2 :int = 2
let myFunBad (a: decimal) b c:decimal = a + b + c

格式设置的空行

  • 单独顶级函数和类定义包含两个空白行。
  • 方法定义的类的内部由一个空行分隔。
  • 可能会 (谨慎) 使用额外的空白行到单独的组相关的函数。 一系列相关一行式命令 (例如,一组虚拟实现) 之间,可以忽略空白行。
  • 使用空白行在函数中,尽量少,以指示逻辑部分。

格式设置的注释

通常将多个双斜杠注释为首 ML 样式块注释。

// Prefer this style of comments when you want
// to express written ideas on multiple lines.

(*
    ML-style comments are fine, but not a .NET-ism.
    They are useful when needing to modify multi-line comments, though.
*)

内联注释应的首字母大写。

let f x = x + 1 // Increment by one.

命名约定

使用驼峰式大小写的类绑定、 表达式绑定和绑定模式的值和函数

通常会接受F#的绑定作为本地变量或在模式匹配的所有名称和函数定义采用驼峰式大小写样式。

// OK
let addIAndJ i j = i + j

// Bad
let addIAndJ I J = I+J

// Bad
let AddIAndJ i j = i + j

本地绑定类中的函数还应使用驼峰式大小写。

type MyClass() =

    let doSomething () =

    let firstResult = ...

    let secondResult = ...

    member x.Result = doSomething()

有关绑定到模块的公共函数使用驼峰式大小写

当模块绑定函数是一个公共 API 的一部分时,它应使用驼峰式大小写:

module MyAPI =
    let publicFunctionOne param1 param2 param2 = ...

    let publicFunctionTwo param1 param2 param3 = ...

使用驼峰式大小写的内部和专用模块绑定值和函数

对于专用模块绑定值,其中包括使用驼峰式大小写:

  • 在脚本中的即席函数

  • 值组成的模块或类型的内部实现

let emailMyBossTheLatestResults =
    ...

对参数使用驼峰式大小写

所有参数应都使用驼峰式大小写,根据.NET 命名约定。

module MyModule =
    let myFunction paramOne paramTwo = ...

type MyClass() =
    member this.MyMethod(paramOne, paramTwo) = ...

模块使用 pascal 命名法

(顶级、 内部、 专用、 嵌套) 的所有模块都应都使用 pascal 命名法。

module MyTopLevelModule

module Helpers =
    module private SuperHelpers =
        ...

    ...

类型声明、 成员和标签的使用 pascal 命名法

类、 接口、 结构、 枚举、 委托、 记录和可区分的联合所有的命名应当使用 pascal 命名法。 类型和标签的记录和可区分的联合中的成员还应使用 pascal 命名法。

type IMyInterface =
    abstract Something: int

type MyClass() =
    member this.MyMethod(x, y) = x + y

type MyRecord = { IntVal: int; StringVal: string }

type SchoolPerson =
    | Professor
    | Student
    | Advisor
    | Administrator

为.NET 中的内部构造使用 pascal 命名法

命名空间、 异常、 事件和项目 /.dll名称还应使用 pascal 命名法。 不仅这会使来自其他.NET 语言的消耗感觉更自然向使用者,也是与你可能会遇到的.NET 命名约定保持一致。

避免在名称中的下划线

从历史上看,一些F#库的名称中使用下划线。 但是,这是不能再广受认可,部分原因是因为它与.NET 命名约定冲突。 话虽如此,一些F#程序员出于历史原因,很大程度、 一定程度上使用下划线和容差和方面非常重要。 但是,请注意样式通常会不喜欢的其他用户可以选择要使用它。

一些例外情况包括与本机组件交互下划线很常见。

使用标准F#运算符

以下运算符定义中F#标准库,应使用而不是定义等效项。 建议使用这些运算符,因为它往往会使代码更具可读性且惯用。 具有背景的 OCaml 或其他功能的编程语言的开发人员可能习惯于不同编程惯例。 以下列表总结了推荐的F#运算符。

x |> f // Forward pipeline
f >> g // Forward composition
x |> ignore // Discard away a value
x + y // Overloaded addition (including string concatenation)
x - y // Overloaded subtraction
x * y // Overloaded multiplication
x / y // Overloaded division
x % y // Overloaded modulus
x && y // Lazy/short-cut "and"
x || y // Lazy/short-cut "or"
x <<< y // Bitwise left shift
x >>> y // Bitwise right shift
x ||| y // Bitwise or, also for working with “flags” enumeration
x &&& y // Bitwise and, also for working with “flags” enumeration
x ^^^ y // Bitwise xor, also for working with “flags” enumeration

前缀语法用于泛型 (Foo<T>) 优先于后缀语法 (T Foo)

F#继承这两个后缀机器学习的样式命名泛型类型 (例如, int list) 以及.NET 样式的前缀 (例如, list<int>)。 .NET 样式,除了五个特定类型为首选项:

  1. 有关F#列表中,使用后缀形式:int list而非list<int>。
  2. 有关F#选项,请使用后缀形式:int option而非option<int>。
  3. 有关F#值选项,请使用后缀形式:int voption而非voption<int>。
  4. 有关F#数组,使用语法名称int[]而非int array或array<int>。
  5. 对于引用单元格,请使用int ref而非ref<int>或Ref<int>。

对于所有其他类型,请使用前缀形式。

格式设置的元组

元组实例化应该是用圆括号括起来,和中分隔的逗号应后跟一个空格,例如: (1, 2), (x, y, z)。

它通常被接受以忽略在模式匹配的元组中的括号:

let (x, y) = z // Destructuring
let x, y = z // OK

// OK
match x, y with
| 1, _ -> 0
| x, 1 -> 0
| x, y -> 1

它通常也接受可以省略括号,如果元组是一个函数的返回值:

// OK
let update model msg =
    match msg with
    | 1 -> model + 1, []
    | _ -> model, [ msg ]

总之,更喜欢用圆括号括起来的元组实例化,但当使用模式匹配或返回值元组,它被视为可以避免括号。

格式设置可区分联合声明

缩进|由 4 个空格的类型定义中:

// OK
type Volume =
    | Liter of float
    | FluidOunce of float
    | ImperialPint of float

// Not OK
type Volume =
| Liter of float
| USPint of float
| ImperialPint of float

格式设置可区分联合

将拆分到多个行的实例化的可区分联合应为包含的数据提供具有缩进的新作用域:

let tree1 =
    BinaryNode
        (BinaryNode(BinaryValue 1, BinaryValue 2),
         BinaryNode(BinaryValue 3, BinaryValue 4))

右括号还可以在新的一行:

let tree1 =
    BinaryNode(
        BinaryNode(BinaryValue 1, BinaryValue 2),
        BinaryNode(BinaryValue 3, BinaryValue 4)
    )

格式设置的记录声明

缩进{类型中定义由 4 空格和同一行上开始的字段列表:

// OK
type PostalAddress =
    { Address: string
      City: string
      Zip: string }
    member x.ZipAndCity = sprintf "%s %s" x.Zip x.City

// Not OK
type PostalAddress =
  { Address: string
    City: string
    Zip: string }
    member x.ZipAndCity = sprintf "%s %s" x.Zip x.City

// Unusual in F#
type PostalAddress =
    {
        Address: string
        City: string
        Zip: string
    }

将打开标记放置在一个新行和新行中的右令牌是更可取,如果要声明接口实现或成员的记录:

// Declaring additional members on PostalAddress
type PostalAddress =
    {
        Address: string
        City: string
        Zip: string
    } with
    member x.ZipAndCity = sprintf "%s %s" x.Zip x.City

type MyRecord =
    {
        SomeField: int
    }
    interface IMyInterface

格式设置的记录

可以在一行中编写短记录:

let point = { X = 1.0; Y = 0.0 }

较长的记录标签应使用新行:

let rainbow =
    { Boss = "Jeffrey"
      Lackeys = ["Zippy"; "George"; "Bungle"] }

放置在打开令牌在新的一行,内容选项卡式通过一个作用域,并置于新行的结束标记是如果您是更可取:

  • 四处移动记录,在代码中使用不同的缩进作用域
  • 通过管道将它们传递到函数
let rainbow =
    {
        Boss1 = "Jeffrey"
        Boss2 = "Jeffrey"
        Boss3 = "Jeffrey"
        Boss4 = "Jeffrey"
        Boss5 = "Jeffrey"
        Boss6 = "Jeffrey"
        Boss7 = "Jeffrey"
        Boss8 = "Jeffrey"
        Lackeys = ["Zippy"; "George"; "Bungle"]
    }

type MyRecord =
    {
        SomeField: int
    }
    interface IMyInterface

let foo a =
    a
    |> Option.map (fun x ->
        {
            MyField = x
        })

相同的规则适用于列表和数组元素。

设置复制更新记录表达式格式

复制更新记录表达式仍然是一条记录,因此类似的准则适用于。

短表达式可以适合某个行:

let point2 = { point with X = 1; Y = 2 }

较长的表达式应使用新行:

let rainbow2 =
    { rainbow with
        Boss = "Jeffrey"
        Lackeys = ["Zippy"; "George"; "Bungle"] }

和记录的指导,您可能想要将专用的大括号的单独的行和缩进到右侧的表达式的一个作用域。 请注意,在某些特殊情况,例如 wrapping 具有不带括号,可选的值中你可能需要保留在同一行的大括号:

type S = { F1: int; F2: string }
type State = { F:  S option }

let state = { F = Some { F1 = 1; F2 = "Hello" } }
let newState =
    {
        state with
            F = Some {
                    F1 = 0
                    F2 = ""
                }
    }

格式设置的列表和数组

编写x :: l与周围的空格::运算符 (::为中缀运算符,因此由空格括起来)。

列表和在单个行上声明的数组应具有一个空格后的左括号和右括号之间:

let xs = [ 1; 2; 3 ]
let ys = [| 1; 2; 3; |]

始终使用两个不同的大括号类似于运算符之间至少一个空格。 例如,将保留之间有空格[和一个{。

// OK
[ { IngredientName = "Green beans"; Quantity = 250 }
  { IngredientName = "Pine nuts"; Quantity = 250 }
  { IngredientName = "Feta cheese"; Quantity = 250 }
  { IngredientName = "Olive oil"; Quantity = 10 }
  { IngredientName = "Lemon"; Quantity = 1 } ]

// Not OK
[{ IngredientName = "Green beans"; Quantity = 250 }
 { IngredientName = "Pine nuts"; Quantity = 250 }
 { IngredientName = "Feta cheese"; Quantity = 250 }
 { IngredientName = "Olive oil"; Quantity = 10 }
 { IngredientName = "Lemon"; Quantity = 1 }]

相同原则也适用于列表或数组的元组。

列表和拆分到多个行的数组记录一样遵循类似的规则:

let pascalsTriangle =
    [|
        [| 1 |]
        [| 1; 1 |]
        [| 1; 2; 1 |]
        [| 1; 3; 3; 1 |]
        [| 1; 4; 6; 4; 1 |]
        [| 1; 5; 10; 10; 5; 1 |]
        [| 1; 6; 15; 20; 15; 6; 1 |]
        [| 1; 7; 21; 35; 35; 21; 7; 1 |]
        [| 1; 8; 28; 56; 70; 56; 28; 8; 1 |]
    |]

和与记录,在自己的行上声明左、 右括号将简化移动代码和到函数中的管道。

如果格式设置表达式

条件语句的缩进取决于把它们组合起来的表达式的大小。 如果cond,e1和e2简短,只需将它们写在同一行中:

if cond then e1 else e2

如果任一cond,e1或e2更长时间,但不多的行:

if cond
then e1
else e2

如果是多行的任何表达式:

if cond then
    e1
else
    e2

使用多个条件语句elif并else都在同一作用域为缩进if:

if cond1 then e1
elif cond2 then e2
elif cond3 then e3
else e4

模式匹配构造

使用|无缩进匹配项的每个子句。 如果表达式很短,则可以考虑使用单独的一行,如果每一子表达式也非常简单。

// OK
match l with
| { him = x; her = "Posh" } :: tail -> x
| _ :: tail -> findDavid tail
| [] -> failwith "Couldn't find David"

// Not OK
match l with
    | { him = x; her = "Posh" } :: tail -> x
    | _ :: tail -> findDavid tail
    | [] -> failwith "Couldn't find David"

如果在模式匹配箭头右侧的表达式太大,则将其移动到以下行,缩进一个步骤,从match / |。

match lam with
| Var v -> 1
| Abs(x, body) ->
    1 + sizeLambda body
| App(lam1, lam2) ->
    sizeLambda lam1 + sizeLambda lam2

模式匹配的匿名函数,通过启动function,应通常不缩进太远。 例如,按如下所示缩进一个作用域是没问题:

lambdaList
|> List.map (function
    | Abs(x, body) -> 1 + sizeLambda 0 body
    | App(lam1, lam2) -> sizeLambda (sizeLambda 0 lam1) lam2
    | Var v -> 1)

通过定义的函数中的模式匹配let或let rec应在启动后的缩进的 4 个空格let,即使function使用关键字:

let rec sizeLambda acc = function
    | Abs(x, body) -> sizeLambda (succ acc) body
    | App(lam1, lam2) -> sizeLambda (sizeLambda acc lam1) lam2
    | Var v -> succ acc

我们不建议对齐箭头。

格式设置 try / with 表达式

模式匹配异常类型应为相同的级别缩进with。

try
    if System.DateTime.Now.Second % 3 = 0 then
        raise (new System.Exception())
    else
        raise (new System.ApplicationException())
with
| :? System.ApplicationException ->
    printfn "A second that was not a multiple of 3"
| _ ->
    printfn "A second that was a multiple of 3"

格式设置函数参数应用程序

一般情况下,大多数函数参数应用程序可在同一行。

如果你想要应用到新行上的函数的参数,将它们缩进按一个作用域。

// OK
sprintf "\t%s - %i\n\r"
     x.IngredientName x.Quantity

// OK
sprintf
     "\t%s - %i\n\r"
     x.IngredientName x.Quantity

// OK
let printVolumes x =
    printf "Volume in liters = %f, in us pints = %f, in imperial = %f"
        (convertVolumeToLiter x)
        (convertVolumeUSPint x)
        (convertVolumeImperialPint x)

适用的 lambda 表达式作为函数参数相同的准则。 如果 lambda 表达式,主体的主体可以拥有另一个行,缩进一个作用域

let printListWithOffset a list1 =
    List.iter
        (fun elem -> printfn "%d" (a + elem))
        list1

// OK if lambda body is long enough
let printListWithOffset a list1 =
    List.iter
        (fun elem ->
            printfn "%d" (a + elem))
        list1

但是,如果 lambda 表达式的主体是多个行,请考虑一下分离到单独的函数而不是具有多行构造为单个参数应用于函数。

格式设置中缀运算符

由空格的单独运算符。 是此规则的例外明显!和.运算符。

中缀表达式是同一列上的配置列表确定:

acc +
(sprintf "\t%s - %i\n\r"
     x.IngredientName x.Quantity)

let function1 arg1 arg2 arg3 arg4 =
    arg1 + arg2 +
    arg3 + arg4

格式设置管道运算符

管道|>运算符应发送到下对它们进行操作的表达式。

// Preferred approach
let methods2 =
    System.AppDomain.CurrentDomain.GetAssemblies()
    |> List.ofArray
    |> List.map (fun assm -> assm.GetTypes())
    |> Array.concat
    |> List.ofArray
    |> List.map (fun t -> t.GetMethods())
    |> Array.concat

// Not OK
let methods2 = System.AppDomain.CurrentDomain.GetAssemblies()
            |> List.ofArray
            |> List.map (fun assm -> assm.GetTypes())
            |> Array.concat
            |> List.ofArray
            |> List.map (fun t -> t.GetMethods())
            |> Array.concat

格式设置模块

本地模块中的代码必须将缩进,相对于该模块,但顶级模块中的代码不应缩进。 Namespace 元素无需将缩进。

// A is a top-level module.
module A

let function1 a b = a - b * b
// A1 and A2 are local modules.
module A1 =
    let function1 a b = a*a + b*b

module A2 =
    let function2 a b = a*a - b*b

格式设置对象表达式和接口

应使用相同的方式对齐对象表达式和接口member要缩进后 4 个空格。

let comparer =
    { new IComparer<string> with
          member x.Compare(s1, s2) =
              let rev (s: String) =
                  new String (Array.rev (s.ToCharArray()))
              let reversed = rev s1
              reversed.CompareTo (rev s2) }

格式表达式中的空白区域

避免多余空白区域,在F#表达式。

// OK
spam (ham.[1])

// Not OK
spam ( ham.[ 1 ] )

命名的参数也不应具有空间周围=:

// OK
let makeStreamReader x = new System.IO.StreamReader(path=x)

// Not OK
let makeStreamReader x = new System.IO.StreamReader(path = x)

格式设置特性

属性位于上方构造:

[<SomeAttribute>]
type MyClass() = ...

[<RequireQualifiedAccess>]
module M =
    let f x = x

[<Struct>]
type MyRecord =
    { Label1: int
      Label2: string }

参数格式设置特性

属性也可以是参数上的位置。 在这种情况下,将它们放置在作为参数并在名称前在同一行上:

// Defines a class that takes an optional value as input defaulting to false.
type C() =
    member __.M([<Optional; DefaultParameterValue(false)>] doSomething: bool)

格式设置多个属性

当多个属性应用于一个构造,它不是参数时,应将它们放,只有每行一个特性:

[<Struct>]
[<IsByRefLike>]
type MyRecord =
    { Label1: int
      Label2: string }

应用于参数时,它们必须在同一行,分隔;分隔符。

设置文本的格式

F#文字使用Literal属性应置于各自的行的属性,然后使用 pascal 命名法命名:

[<Literal>]
let Path = __SOURCE_DIRECTORY__ + "/" + __SOURCE_FILE__

[<Literal>]
let MyUrl = "www.mywebsitethatiamworkingwith.com"

避免在值所在的同一行上放置属性。

历史资料。此文对应 2019-08-18 的中文文档快照,适用产品版本和命令行为以原文为准。

来源与署名:Microsoft 和文档贡献者。固定版本原文。文档采用 CC BY 4.0,示例代码采用 MIT 授权。本站将 Markdown 转为 HTML,规范化链接,移除外部媒体和导航标记。

资料来源与适用范围

资料来源
Microsoft / .NET 中文文档历史快照
适用范围
历史中文快照 2019-08-18;详见原文产品版本;文档日期 02/08/2019
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-16
授权说明
CC BY 4.0(文档)/ MIT(示例代码)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。