Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

f#

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 ?

like image 253
KallDrexx Avatar asked Mar 24 '23 22:03

KallDrexx


1 Answers

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.

like image 154
N_A Avatar answered Apr 06 '23 17:04

N_A