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 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.
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) = // ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With