Should F# functions be placed in modules, classes, or another structure? [closed]
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this questionI'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. Unfortunate开发者_运维问答ly 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?
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 toList.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 usePascalCase
(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) = // ...
concerning the design of F# components there is a very good draft online.
JPalmer allready pointed you to the syntay problems but I think some other questions deserve more:
What is a module?
Yes JPalmer is right - modules are compiled into static classes but do we really care inside F#? IMHO you should use more modules than classes when programming in F#. In OOP you define your classes and the methods within. In FP you define simple types (without behaviour) and a bunch of functions to transform them. And the natural place to collect those functions is the module.
Is it like a class (something like a VB.NET module) or is it something else altogether?
A VB module is indeed a good comparision.
If something else, then are there classes in F#?
Yes you can use classes in F# - it's a complete .net languague and .net is OOP. You can do practically everything in F# you could do in C# of VB.net (only certain cases generic constraints can be a pain)
Are there other structures that I should be using instead? No - collect your functions into modules but of course use records and abstract data-types for your data.
How do I declare a module?
Have a look at the online docs: Modules (F#) - there you will find everything you need.
What is a module:
A module is compiled down to a static class. But I think of modules as being analogous to namespaces in C#
there are classes in F# - use
type SomeType(constructor,args) =
....
If you have
namespace Name
module Mod
....
this won't compile - as you know, you can use a few alternatives
module Namespace.Module
as the first line in the file
or
namespace Name
module Mod =
....
精彩评论