定义F#函数但不调用它,函数似乎仍然被调用

时间:2016-02-28 15:22:16

标签: f#

在下面的代码中,为什么函数printStatement被调用?

module Tests

let printStatement =
    printfn "%s" "statement"

let functionCallingPrintStatement =
    printStatement

let functionNotCallingPrintStatement =
    printfn "%s" "This is a test"

如果我在主程序中调用functionNotCallingPrintStatement

open Tests

[<EntryPoint>]
let main argv =
    functionNotCallingPrintStatement     
    0 

这是输出:

  

声明

     

这是一个测试

如果我将函数functionNotCallingPrintStatement更改为:

let functionNotCallingPrintStatement =
    ()

第一个陈述被压制。

1 个答案:

答案 0 :(得分:5)

它们不是函数,它们是unit

类型的值
val functionNotCallingPrintStatement : unit = ()

要使它们成为unit -> unit个函数,请添加括号

let functionNotCallingPrintStatement () =
    printfn "%s" "This is a test"

val functionNotCallingPrintStatement : unit -> unit