Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do F# types transfer over to C#?

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?

like image 567
Joan Venge Avatar asked Oct 01 '10 18:10

Joan Venge


3 Answers

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#
like image 102
Brian Avatar answered Oct 16 '22 08:10

Brian


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.

like image 42
Tomas Petricek Avatar answered Oct 16 '22 08:10

Tomas Petricek


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.

like image 1
desco Avatar answered Oct 16 '22 10:10

desco