Should F# functions be placed in modules, classes, or another structure? [closed]

To give some specific recommendations about choosing between namespaces, modules abd classes in F#:

  • If you’re writing functions using let that are expected to be used from F#, then putting them inside a module is the best choice. This gives you API similar to List.map and other basic F# functions.

    Regarding naming, you should use camelCase unless you expect C# users to call the functions too. In that case, you should use PascalCase (and note that module will be compiled to a static class).

  • If you’re writing type delcarations, then these should generally be placed in a namespace. They are allowed inside modules too, but then they’ll be compiled as nested classes.

  • If you’re writing F# classes, then they should be placed in namespaces too. In generall, if you’re writing F# code that will be called by C#, then using classes is the best mechanism as you get full control of what the user will see (F# class is compiled to just a class).

If you have a file, it can either start with namespace Foo.Bar or module Foo.Bar, which places all code in the file inside a namespace or a module. You can always nest more modules inside this top-level declaration. A common pattern is to start with a single namespace and then include some type and module declarations in the file:

namespace MyLibrary

type SomeType = 
  // ...

module SomeFuncs = 
  let operation (st:SomeType) = // ...

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)