FSharp naming conventions for types

88 views
Skip to first unread message

Casper Bollen

unread,
Nov 20, 2015, 5:40:13 AM11/20/15
to F# Discussions
FSharp naming conventions require type definitions to be PascalCase. However, module names also need to be PascalCase. This can lead to name collisions. I generally like to create first types and then the modules that handle those types, like:

type mytype = MyType of string

module MyType =

    let create
(s: string) = s.Trim() |> MyType

// Usage
let x
= MyType.create "Test"

I get that when this code needs to be exposed then it should adhere the general .net guidelines. But for internal use I think that all lower case (or camelCase) type naming is more convenient.

I don't know if there are other compelling reasons to stick to PascalCase for types?

Karl Nilsson

unread,
Nov 20, 2015, 5:48:30 AM11/20/15
to F# Discussions
You may be able to avoid the name collision by using the [<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>] attribute on your module declaration.

--
--
To post, send email to fsharp-o...@googlegroups.com
To unsubscribe, send email to
fsharp-opensou...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/fsharp-opensource
---
You received this message because you are subscribed to the Google Groups "F# Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fsharp-opensou...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michael Newton

unread,
Nov 20, 2015, 5:48:49 AM11/20/15
to F# Discussions
Hi Casper,
this is purely convention, but personally I do think it helps discoverability to have the type and the module with handlers with the same name.

To avoid the name collision issues, you can do the following which is how a lot of the modules in the core libraries are declared

type Thing = Thing of string

[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module Thing =
    let create str = Thing str

This gives the type and module unique names in IL, but allows them both to be accessed from F# code with the same name.

Thanks,

Michael

On Fri, 20 Nov 2015 at 10:40 Casper Bollen <hal...@gmail.com> wrote:
--

Casper Bollen

unread,
Nov 20, 2015, 8:47:43 AM11/20/15
to F# Discussions
Thanks, great advice, going to reconsider my naming convention.

Steven Ramsay

unread,
Nov 24, 2015, 5:44:44 AM11/24/15
to fsharp-o...@googlegroups.com
I use this attribute heavily in all my F#.  Is there a good reason why the default is the way that it is?  

For me personally, life would be easier if the compiler behaved as if this attribute was automatically on every module definition.  Obviously it is not a big deal to have to type it out but I think it scares people off F# a bit when they look at some code that ought to be simple but it has these complicated looking compiler directives all over the place.

Best,
Steven

--
Steven Ramsay


Date: Fri, 20 Nov 2015 05:47:43 -0800
From: hal...@gmail.com
To: fsharp-o...@googlegroups.com
Subject: Re: FSharp naming conventions for types

Loïc Denuzière

unread,
Nov 24, 2015, 9:38:35 AM11/24/15
to F# Discussions, steven...@live.com
Well the thing is, this attribute actually compiles a module called `Foo` into a .NET class called `FooModule` instead of just `Foo`, so having it by default would be pretty bad for code intended to be called from other languages. For this reason it should be opt-in.

Maybe one thing that could be done is make this attribute usable at the assembly level, so that you only need to opt in once for all the modules in your project.
Reply all
Reply to author
Forward
0 new messages