简体   繁体   中英

Why isn't a tuple parameter being recognized on my function?

Why isn't a tuple parameter being recognized on my function?

I have the following function:

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

Note how the last argument of the function is a tuple:

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

I then attempt to invoke this function using the following:

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

Yet, it's stating that it's not expecting a tuple. Instead, it's expecting:

Expecting a (string -> string -> unit) -> string -> 'a

The complete error is here:

Type mismatch. Expecting a (string -> string -> unit) -> string -> 'a but given a (string -> string -> unit) * string -> unit The type 'string -> string -> unit' does not match the type '(string -> string -> unit) * string'

Here's the code:

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

||> unpacks a tuple 'a * 'b to call a normal function 'a -> 'b -> 'c ; as shown, you don't want this unpacking behavior, as you have 'a * 'b -> 'c .

Either change ||> to |> so the tuple is passed directly:

(writeToFile, file) |> deposit OneDollarBill

Or change deposit to accept curried arguments rather than a tuple:

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

(writeToFile, file) ||> deposit OneDollarBill

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM