F# 类型如何转移到 C#?
如果在 F# 中我有这样的方法:
drawBox
drawSphere
paintImage
它们是否会转移到 C#,完全相同?
如果是这样的话,那么它是否会损害 C# 中的命名约定(其中方法应该采用 PascalCase)?
或者我应该在 F# 中使用 PascalCase 来解决这个问题?
If in F# I have methods like:
drawBox
drawSphere
paintImage
Would they be transferred to C#, exactly the same?
If that's the case, then wouldn't it compromise the naming conventions in C#, where methods are supposed to be PascalCase?
Or should I make them PascalCase in F# as well to remedy this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该遵循 F#组件设计指南。
值得注意的部分是:
驼峰命名法通常用于设计为不合格使用的公共函数(例如
invalidArg
),以及“标准集合函数”(例如List.map
)。在这两种情况下,函数名称的作用非常类似于语言中的关键字。另一种划分方式:
You should follow the F# Component Design Guidelines.
The notable parts are:
camelCase is generally used for public functions which are designed to be used unqualified (e.g.
invalidArg
), and for the "standard collection functions" (e.g.List.map
). In both these cases, the function names act much like keywords in the language.Another way to slice it:
正如其他人已经指出的那样,编译成员的名称与您在 F# 代码中编写的名称完全相同。一般来说,声明类时遵循标准 C# 命名约定是个好主意。声明 F# 模块时,您可以使用
camelCase
(特别是对于面向 F# 用户的模块)或PascalCase
(如果您想在 C# 中使用它们)。此外,您还可以使用一个技巧(该技巧在 F# 核心库中用于
List.map
等实际编译为List.Map
的函数)。您可以使用 CompiledName 属性来指定编译版本中的名称:使用统一的命名约定可能更好,但如果您想为 F# 用户和标准 .NET 保留一个漂亮的短名称对于 C# 用户来说,这是一个有趣的选项。
As others already pointed out, the names of compiled members are exactly the same as the names you wrote in the F# code. In general, it is a good idea to follow standard C# naming conventions when declaring classes. When declaring an F# module then you can either use
camelCase
(especially for modules that are intended for F# users) orPascalCase
if you want to use them from C#.Also, there is one trick that you can use (this is used in the F# core library for functions like
List.map
that are actually compiled asList.Map
). You can use theCompiledName
attribute to specify the name in the compiled version:It is probably better to use a unified naming convention, but if you want to keep a nice short name for F# users and standard .NET name for C# users, this is an interesting option.
F# 和 C# 中的类型方法名称相同。如果您希望在 C# 中使用 PascalCase - 您应该在 F# 中使用此命名约定,与驼峰命名法相同。
type methods names will be the same both in F# and C#. if you want PascalCase in C# - you should use this naming convention in F#, the same with camelCase.
命名约定是针对人类的。编译器和运行时实际上并不关心每个变量名称是否完全不可读,它们仍然可以工作。
Naming conventions are for humans. Compilers and runtimes really don't care if every variable name is entirely unreadable, they'll still work.