[ACCEPTED]-What the difference between a namespace and a module in F#?-namespaces

Accepted answer
Score: 100

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

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

open ModuleName
Thing  // rather than ModuleName.Thing

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

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

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

More Related questions