F # Output type with curry functions

I have the following code

let bar foo baz = foo, baz 

let z = bar 3
let z1 = z 2

      

however If I comment out the last line let z1 = z 2

I get the error

let z = bar 3
----^

stdin(78,5): error FS0030: Value restriction. 
The value 'z' has been inferred to have generic type
val z : ('_a -> int * '_a)    
Either make the arguments to 'z' explicit or, if you do not intend for it to be generic, 
add a type annotation.

      

I am completely lost on how to annotate the function correctly.

+3


source to share


1 answer


What you are running into is an aspect of F # called value constraint. This essentially means that if you define a value that is not syntactically a function, then it cannot be generic.

let bar foo baz = foo, baz

      

... is syntactically a function (it obviously has two arguments) and is therefore deduced as generic. However:

let z = bar 3

      

... is not syntactically a function - it's just a value z

that turns out to be a function (since it indicates the type bar

). Therefore, it cannot be general. In your snippet, the overall limitation was limited to the following line:

let z1 = z 2

      



This captures the type z

as int -> int * int

. If you don't have this line, you can set the type yourself:

let z : int -> int * int = bar 3

      

Or you can do it with a syntax function that can be output as generic:

let z a = bar 3 a

      

For more information, look for various other questions and answers that discuss value constraints .

+6


source







All Articles