Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm starting to code in F# and am calling functions from functions with functions as parameters - there are plenty of learning resources online. Now I am trying to put together the pieces into something more than just a collection of functions. Unfortunately I'm not finding many resources dealing with structure, design, or even how the 'bits' tie together.

I've found the namespace keyword (e.g. namespace MyOnlyNamespace) but I get a compiler error on the functions that I've placed inside the namespace:

Namespaces cannot contain values. Consider using a module to hold your value declarations.

When I add module CoolFunctions I get

Unexpected start of structured construct in definition. Expected '=' or other token

So I have a multi-part question (but please answer any part that you can)

  • What is a module?
  • Is it like a class (something like a VB.NET module) or is it something else altogether?
  • If something else, then are there classes in F#?
  • Are there other structures that I should be using instead?
  • How do I declare a module?
like image 922
Kirk Broadhurst Avatar asked Sep 26 '11 04:09

Kirk Broadhurst


People also ask

What the fk should I make for dinner book?

What the F*@# Should I Make For Dinner? gets everyone off their a**es and in the kitchen. Derived from the incredibly popular website, whatthefuckshouldimakefordinner.com, the book functions like a Choose your own adventure" cookbook, with options on each page for another f*@#ing idea for dinner.


1 Answers

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) = // ... 
like image 86
Tomas Petricek Avatar answered Oct 04 '22 16:10

Tomas Petricek