views:

772

answers:

1

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)?

+11  A: 

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. :)

Brian