Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# naming convention

Is there an "official" naming / casing convention for F#?

I'm always in doubt of using C# style or not:

Class.MyFunctionName or Module.my_function_name

In F# you're meant to mix BCL classes and F# library ones: they have different casing and the code looks very ugly.

like image 284
Luca Martinetti Avatar asked Feb 09 '09 01:02

Luca Martinetti


People also ask

Facebook itu apa sih?

Facebook adalah media sosial dan layanan jejaring sosial online Amerika yang dimiliki oleh Meta Platforms.


3 Answers

Yes, there is confusion, because F# has morphed from OCaml to .Net over the years. Basically, the naming conventions are a "breaking change" - old code is inconsistent with new code.

However, the May 2009 CTP has settled the issue.

The Release Notes say...

Standard Library Naming Conventions

The naming conventions adopted for the F# library are as follows:

  • All .NET and F# OO code uses PascalCase according to existing .NET guidelines

  • The F# functional programming operators such as List.map are for use in F# internal implementation code. This kind of code uses camelCase for operator names

  • Underscores should not be used.

So, your question...

Class.MyFunctionName or Module.my_function_name 

The answer is

Class.MyFunctionName and Module.MyFunctionName (but see edit below!)

(applying rule 1 above).

-- Edit. Nov 2, 2019 --

The current guidelines recommend camelCase for functions at module level, so it's

Module.myFunctionName

Which then makes production code consistent with the F# libraries (eg. List.averageBy)

like image 197
Stephen Hosking Avatar answered Sep 18 '22 22:09

Stephen Hosking


I think the answer might have changed since the time of the currently accepted answer.

The F# Style Guide today says:

Use PascalCase for type declarations, members, and labels

Classes, interfaces, structs, enumerations, delegates, records, and discriminated unions should all be named with PascalCase. Members within types and labels for records and discriminated unions should also use 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

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

and

Use camelCase for module-bound public functions

When a module-bound function is part of a public API, it should use camelCase: F#

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

    let publicFunctionTwo param1 param2 param3 = ...

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

So, based on those, my answer is that this:

Class.MyFunctionName 
Module.my_function_name 

Should be written like this:

Class.MyFunctionName  
Module.myFunctionName 
like image 25
Sean Kearon Avatar answered Sep 19 '22 22:09

Sean Kearon


Anything official: I think "not quite yet", but whenever VS 2010 reaches Beta1 you'll probably see the F# library in its nearly-final form, and there will be a number of renamings relative to the CTP. F# will probably always be a little more schizophrenic than its older .NET cousins, given its history.

like image 31
Brian Avatar answered Sep 17 '22 22:09

Brian