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?
You should follow the F# Component Design Guidelines.
The notable parts are:
- Do use the .NET naming and capitalization conventions for object-oriented code, including F#-facing libraries.
- Do use either PascalCase or camelCase for public functions and values in F# modules.
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:
- members and types should always be PascalCase, just like 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#
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) or PascalCase
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 as List.Map
). You can use the CompiledName
attribute to specify the name in the compiled version:
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.
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.
精彩评论