3

Let's say we have the following function (body isn't very important):

let writelogf greeting x = 
    Printf.printf "%A" greeting
    Printf.printf "%A" x

The type of this function is 'a -> 'b -> unit which is what I want.

Now I define a partially applied function:

let logf x = writelogf "Hello!" x

The type of this function is 'a -> unit which is what I expect. I'm able to use it with arguments of any type:

logf "Test"
logf 1

But if I try to simplify the declaration and repeat the calls:

let logf2 = writelogf "Hello!"
logf2 "Test"
logf2 1

it won't compile anymore, because logf2 is not generalized, it has type string -> unit (infered from first usage).

Why is that? If I have a function with 5 parameters and I need to partially apply one, do I have to repeat the other 4?

Mikhail Shilkov
  • 34,128
  • 3
  • 68
  • 107

1 Answers1

3

In the case where you have written:

let logf2 = writelogf "Hello!"

You are effectively binding a value, the type of which is generic. If you change your definition into a function, e.g. by taking the argument explicitly, or an argument of unit type, you can resolve the issue.

let logf x = writelogf "Hello!" x

let logf2() = writelogf "Hello!"

If you have a function with 5 parameters and you partially apply one, again you will need to take at least one argument explicitly but you do not need to repeat the other 3.

let test5 a b c d e =
    printfn "%A" (a, b, c, d, e)

let testApplyOne x = test5 "Hello!" x
TheInnerLight
  • 12,034
  • 1
  • 29
  • 52