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

自古美人都是妖i 提交于 2020-01-23 15:36:09

问题


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 ?


回答1:


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.



来源:https://stackoverflow.com/questions/16620368/why-are-all-my-functions-being-run-even-though-im-only-calling-one-function-in

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!