Tuesday, May 17, 2011

Studying F# : Module

In F#, there are namespace like C#. But, it is not normal F# way and F#'s namespace is not permitted nested namespaces. The normal F# way is defining module.

Declaration

module Examples
let helloWorld() = "Hello World"
module Greeting =
    let helloWorld() = "Hello Module World"
    module Inner =
        let helloWorld() = "Hello Inner Module World"
Normally, it is enough that there is just one module declaration in the first line. If you want to declare more than one modules or nested modules, module declaration is in below lines and it requires =.

Usage

If you want to use some modules, open statement must be in another module or in namespace global.
namespace Program
open Examples
[<Class>]
type Simple() =
    static member execute() =
        System.Console.WriteLine("Examples.helloWorld() : {0}", helloWorld())
        System.Console.WriteLine("Examples.Greeting.helloWorld() : {0}", Greeting.helloWorld())
        System.Console.WriteLine("Examples.Greeting.Inner.helloWorld() : {0}", Greeting.Inner.helloWorld())
        0

No comments:

Post a Comment