F#命名约定

Luc*_*tti 16 .net f# naming-conventions

F#是否有"官方"命名/套管约定?

我总是怀疑是否使用C#样式:

Class.MyFunctionName or Module.my_function_name
Run Code Online (Sandbox Code Playgroud)

在F#中,你的意思是混合使用BCL类和F#库:它们有不同的外壳,代码看起来非常难看.

Ste*_*ing 22

是的,存在混淆,因为多年来F#已经从OCaml转变为.Net.基本上,命名约定是"重大变化" - 旧代码与新代码不一致.

但是,2009年5月的CTP解决了这个问题.

发行说明说...

标准库命名约定

F#库采用的命名约定如下:

  • 所有.NET和F#OO代码都根据现有的.NET指南使用PascalCase

  • F#函数编程操作符(如List.map)用于F#内部实现代码.这种代码使用camelCase作为运营商名称

  • 不应使用下划线.

所以,你的问题......

Class.MyFunctionName or Module.my_function_name 
Run Code Online (Sandbox Code Playgroud)

答案是

Class.MyFunctionName和Module.MyFunctionName

(适用上述规则1).

通过与F#编程运算符(例如List.averageBy)的比较仍然存在一些混淆,但生产F#代码应该使用CamelCase,因此看起来像其他人的.Net代码.如有疑问,请检查CTP 的示例代码.

(我个人喜欢_the_caml_style,但我必须GetOverThat)


Sea*_*ron 7

我认为自当前接受答案以来答案可能已经改变。

今天的F# 风格指南说:

使用 PascalCase 进行类型声明、成员和标签

类、接口、结构、枚举、委托、记录和可区分联合都应该使用 PascalCase 命名。记录和受歧视联合的类型和标签内的成员也应使用 PascalCase。

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
Run Code Online (Sandbox Code Playgroud)

https://learn.microsoft.com/en-us/dotnet/fsharp/style-guide/formatting#use-pascalcase-for-type-declarations-members-and-labels

对模块绑定的公共函数使用驼峰式命名法

当模块绑定函数是公共 API 的一部分时,应使用驼峰命名法:F#

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

    let publicFunctionTwo param1 param2 param3 = ...
Run Code Online (Sandbox Code Playgroud)

https://learn.microsoft.com/en-us/dotnet/fsharp/style-guide/formatting#use-camelcase-for-module-bound-public-functions

因此,基于这些,我的答案是:

Class.MyFunctionName 
Module.my_function_name 
Run Code Online (Sandbox Code Playgroud)

应该这样写:

Class.MyFunctionName  
Module.myFunctionName 
Run Code Online (Sandbox Code Playgroud)