Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modules v. namespaces in F#

Tags:

f#

I've got a library written in F#, consumed by C# and F#. this library defines a class foo, in module bar, in namespace random:

#light
namespace random

module bar

type foo() = ...

Now, when I go to consume type random.foo.bar, C# intellisense sees it as type bar, nested in type random.foo.

The question is this: Is there an advantage to C# to define externally-consumable code into modules, rather than namespaces? I understand that modules are a good way to group functions, but what about classes?

like image 390
kolosy Avatar asked Jul 20 '09 19:07

kolosy


People also ask

What is the difference between module and namespace?

A module is a way which is used to organize the code in separate files and can execute in their local scope, not in the global scope. A namespace is a way which is used for logical grouping of functionalities with local scoping.

What is the difference between module and namespace in Python?

In Python-speak, modules are a namespace—a place where names are created. And names that live in a module are called its attributes. Technically, modules correspond to files, and Python creates a module object to contain all the names defined in the file; but in simple terms, modules are just namespaces.

What are modules in F?

In the context of F#, a module is a grouping of F# code, such as values, types, and function values, in an F# program.

Are namespaces modules?

Namespaces are simply named JavaScript objects in the global namespace. This makes namespaces a very simple construct to use. Unlike modules, they can span multiple files, and can be concatenated using outFile .


1 Answers

If you're publishing F# components for consumption from other .Net languages, then you should avoid modules in the public interfaces, and stick to namespaces containing classes, structs, and enums.

(Modules are a handy way either to publish values, functions and types among F#-only components, or as 'internal' implementation details of an F# component that publishes .Net classes.)

(Do see also this question for a discussion of the 'technical distinction' between namespaces and modules. This question and my answer above are more about the 'intentional differences' e.g. when you would choose to use each.)

like image 97
Brian Avatar answered Sep 20 '22 19:09

Brian