KallDrexx
KallDrexx

Reputation: 27833

Why are all my functions being run even though I'm only calling one function in one module?

I have the following code in a Test.fs file:

namespace Testing

module test1 =
    let Run =
        printfn "Test1"

module test2 =
    let Run =
        printfn "Test2"

In my Program.fs I am calling:

[<EntryPoint>]
let main argv = 
    let sw = Stopwatch.StartNew()

    printfn "%A" Testing.test1.Run

    sw.Stop()
    printfn "Problem took %d minutes, %d seconds, and %d milliseconds" sw.Elapsed.Minutes sw.Elapsed.Seconds sw.Elapsed.Milliseconds

    let s = Console.ReadLine()
    0 // return an integer exit code

This outputs

Test1

Test2

Why is Test2 being outputting even though I am only calling Test1.Run ?

Upvotes: 4

Views: 231

Answers (1)

N_A
N_A

Reputation: 19897

test1.Run is not a function, it is a value. When you open a module you execute all top-level code in that module. In this case you are defining test1.Run and test2.Run which are both bindings rather than functions.

I can't tell from what you posted exactly what is happening, but it's clear that your main function is not getting called, otherwise printfn "%A" Testing.test1.Run would print <null> and printfn "Problem took %d minutes, %d seconds, and %d milliseconds" sw.Elapsed.Minutes sw.Elapsed.Seconds sw.Elapsed.Milliseconds would print something as well.

Upvotes: 7

Related Questions