Scott Nimrod
Scott Nimrod

Reputation: 11595

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

Upvotes: 0

Views: 100

Answers (1)

ildjarn
ildjarn

Reputation: 62975

||> 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

Upvotes: 3

Related Questions