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


Answers

What is a module in .NET?

A module is a logical collection of code within an Assembly. You can have multiple modules inside an Assembly, and each module can be written in different .NET languages (VS, as far as I'm aware, doesn't support creation of multi-module assemblies).

Assemblies contain modules. Modules contain classes. Classes contain functions.

Yes you can access assemblies, modules, classes, functions, properties, fields etc all via reflection at runtime.

Question

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




In F# what does top-level mean?

I think the term top-level has different meaning in different contexts.

Generally speaking, I'd use it whenever you have some structure that allows nesting to refer to the one position at the top that is not nested inside anything else.

For example, if you said "top-level parentheses" in an expression, it would refer to the outer-most pair of parentheses:

((1 + 2) * (3 * (8)))
^                   ^

When talking about functions and value bindings (and scope) in F#, it refers to the function that is not nested inside another function. So functions inside modules are top-level:

module Foo = 
  let topLevel n = 
    let nested a = a * 10
    10 + nested n

Here, nested is nested inside topLevel.

In F#, functions and values defined using let can appear inside modules or inside classes, which complicates things a bit - I'd say only those inside modules are top-level, but that's probably just because they are public by default.

The do keyword works similarly - you can nest it (although almost nobody does that) and so top-level do that allows STAThread attribute is the one that is not nested inside another do or let:

module Foo =
  [<STAThread>] 
  do
    printfn "Hello!"

Bud it is not allowed on any do nested inside another expression:

do
  [<STAThread>] 
  do 
    printfn "Hello!"
  printfn "This is odd notation, I know..."



A file

That's what a module is.

module: A single file containing content that can be executed by the VES

(Where VES is a program which reads .NET assembly and converts it to machine code.) see http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-335.pdf Partition I page 16.

--

An assembly is coherent collection of files in the filesystem (modules). See http://msdn.microsoft.com/en-us/library/zst29sk2(vs.71).aspx

Obviously class definitions are defined inside the file (module) itelf.







Tags