简体   繁体   English

F#类型如何转移到C#?

[英]How do F# types transfer over to C#?

If in F# I have methods like: 如果在F#中我有以下方法:

drawBox
drawSphere
paintImage

Would they be transferred to C#, exactly the same? 它们会被转移到C#,完全一样吗?

If that's the case, then wouldn't it compromise the naming conventions in C#, where methods are supposed to be PascalCase? 如果是这种情况,那么它不会破坏C#中的命名约定,其中方法应该是PascalCase吗?

Or should I make them PascalCase in F# as well to remedy this? 或者我应该在F#中使用PascalCase来解决这个问题吗?

You should follow the F# Component Design Guidelines . 您应该遵循F#组件设计指南

The notable parts are: 值得注意的部分是:

  • Do use the .NET naming and capitalization conventions for object-oriented code, including F#-facing libraries. 对面向对象的代码使用.NET命名和大写约定,包括F#-facing库。
  • Do use either PascalCase or camelCase for public functions and values in F# modules. 对F#模块中的公共函数和值使用PascalCase或camelCase。 camelCase is generally used for public functions which are designed to be used unqualified (eg invalidArg ), and for the "standard collection functions" (eg List.map ). camelCase通常用于公共函数,设计用于不合格(例如invalidArg ),用于“标准集合函数”(例如List.map )。 In both these cases, the function names act much like keywords in the language. 在这两种情况下,函数名称的作用与语言中的关键字非常相似。

Another way to slice it: 切片的另一种方法:

  • members and types should always be PascalCase, just like C# 成员类型应该始终是PascalCase,就像C#
  • let-bound entities in modules can be camelCase, but this stuff is typically not stuff you'd expose publicly out of a library that is intended to be consumed by C# 模块中的let-bound实体可以是camelCase,但是这些东西通常不是你公开暴露在C#消耗的库中的东西。

As others already pointed out, the names of compiled members are exactly the same as the names you wrote in the F# code. 正如其他人已经指出的那样,编译成员的名称与您在F#代码中编写的名称完全相同。 In general, it is a good idea to follow standard C# naming conventions when declaring classes. 通常,在声明类时遵循标准C#命名约定是个好主意。 When declaring an F# module then you can either use camelCase (especially for modules that are intended for F# users) or PascalCase if you want to use them from C#. 在声明F#模块时,您可以使用camelCase (特别是针对F#用户的模块)或PascalCase如果您想在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 as List.Map ). 此外,还有一个技巧可以使用(这在F#核心库中用于List.map等实际编译为List.Map )。 You can use the CompiledName attribute to specify the name in the compiled version: 您可以使用CompiledName属性在已编译的版本中指定名称:

module Bar = 
  [<CompiledName("Foo")>]
  let foo a = a + 1

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#用户保留标准的.NET名称,这是一个有趣的选择。

type methods names will be the same both in F# and C#. 类型方法名称在F#和C#中都是相同的。 if you want PascalCase in C# - you should use this naming convention in F#, the same with camelCase. 如果你想在C#中使用PascalCase - 你应该在F#中使用这个命名约定,与camelCase相同。

Naming conventions are for humans. 命名约定适用于人类。 Compilers and runtimes really don't care if every variable name is entirely unreadable, they'll still work. 编译器和运行时实际上并不关心每个变量名是否完全不可读,它们仍然可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM