I'm getting a VR error on a let binding on module scope saying one of its parameters is a generic, but I don't know why that parameter is generic in the first place. This is the code:
let private asJsonResponse (responseSource: _ Task) =
fun (next: HttpFunc) (ctx: HttpContext) ->
task {
let! consumption = responseSource
return! json consumption next ctx
}
let getVal = someFuncThatReturnsTaskOfMyType() |> asJsonResponse
The error is on the last line:
error FS0030: Value restriction. The value
getValhas been inferred to have generic typeval getVal: (HttpFunc -> '_a -> Task<HttpContext option>)when'_a :> HttpContextEither make the arguments togetValexplicit or, if you do not intend for it to be generic, add a type annotation.
I understand that it essentially generalizes ctx: HttpContext to something that can be cast to HttpContext. Why does this happen? And why only for this parameter and not next: HttpFunc?
HttpContext is a class and HttpFunc is a function type, is that the problem?
It compiles if you either add type annotations:
Or add explicit parameter:
The simple answer is that type inferring mechanism of the F# compiler isn't perfect. You can read about this here :
and for more specific details, here is the documentation:
MSDN Automatic Generalisation in F# have some examples of it