为什么我的函数没有识别元组参数?

时间:2016-07-17 13:57:50

标签: f#

为什么我的函数没有识别元组参数?

我有以下功能:

let deposit depositType ((logTransaction:string -> string -> unit), (file:string)) =
    depositType |> makeInitialDeposit
                |> sprintf "Deposited: %f"
                |> logTransaction file

注意函数的最后一个参数是一个元组:

((logTransaction:string -> string -> unit), (file:string))

然后我尝试使用以下方法调用此函数:

let file = "c:\\myfile.txt"
(writeToFile, file) ||> deposit OneDollarBill

然而,它表示它并不期待一个元组。相反,它期待:

  

期待a(字符串 - >字符串 - >单位) - > string - > “一

完整的错误在这里:

  

类型不匹配。期待一个       (string - > string - > unit) - > string - > '但是给了一个       (string - > string - > unit)* string - > unit类型'字符串 - > string - > unit'与类型'(string - > string - > unit)* string'

不匹配

以下是代码:

let writeToFile (filePath:string) (message:string) =
    let file = new System.IO.StreamWriter(filePath)
    file.WriteLine(message)
    file.Close()

let makeInitialDeposit deposit =
    deposit |> insert []

let deposit depositType ((logTransaction:string -> string -> unit), (file:string)) =
    depositType |> makeInitialDeposit
                |> sprintf "Deposited: %f"
                |> logTransaction file

let file = "c:\\myfile.txt"
(writeToFile, file) ||> deposit OneDollarBill

1 个答案:

答案 0 :(得分:3)

||>解包元组'a * 'b以调用普通函数'a -> 'b -> 'c;如图所示,您不需要这种解包行为,因为您有'a * 'b -> 'c

||>更改为|>,以便直接传递元组:

(writeToFile, file) |> deposit OneDollarBill

或者更改deposit以接受curried参数而不是元组:

let deposit depositType (logTransaction:string -> string -> unit) (file:string) =
    ...

(writeToFile, file) ||> deposit OneDollarBill