Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What the difference between a namespace and a module in F#?

I've just started learning F# (with little prior experience with .NET) so forgive me for what is probably a very simple question: What the difference between a namespace and a module in F#?

Thanks

Dave

Edit: Thanks for the answer Brian. That's what I wanted to know. Just a clarification: can you also open a namespace as well (similar to C# using statement)?

like image 947
Dave Berk Avatar asked Apr 27 '09 20:04

Dave Berk


People also ask

What is 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.

Is a module a namespace?

A module in TypeScript is a standard ES6 notion, it uses import / export keywords at the top level of the code; A namespace is a notion specific to TypeScript to help to organize the code in an obsolete fashion.

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. Grouping code in modules helps keep related code together and helps avoid name conflicts in your program.


1 Answers

A namespace is a .Net thing, common in many industrial-strength languages, just a way to organize frameworks and avoid naming conflicts among different libraries. Both you and I can define a type "Foo" and use them both in a project, provided they are in different namespaces (e.g. NS1.Foo and NS2.Foo). Namespaces in .Net contain types.

A module is an F# thing, it is roughly analogous to a "static class"... it is an entity that can hold let-bound values and functions, as well as types (note that namespaces cannot directly contain values/functions, namespaces can only contain types, which themselves can contain values and functions). Things inside a module can be referenced via "ModuleName.Thing", which is the same syntax as for namespaces, but modules in F# can also be 'opened' to allow for unqualified access, e.g.

open ModuleName
...
Thing  // rather than ModuleName.Thing

(EDIT: Namespaces can also similarly be opened, but the fact that modules can contain values and functions makes opening a module more 'interesting', in that you can wind up with values and functions, e.g. "cos", being names you can use directly, whereas in other .Net languages you'd typically always have to qualify it, e.g. "Math.cos").

If you type in code at 'the top level' in F#, this code implicitly goes in a module.

Hope that helps somewhat, it's a pretty open-ended question. :)

like image 115
Brian Avatar answered Oct 08 '22 21:10

Brian