Consider this code:
let f<'a when 'a:comparison> (x: 'a) = ()
let g x = f x
let h<'b> (x: 'b) = f x
The function f
has a generic parameter constrained with comparison.
In function g
, I just use f
and let the compiler infer generic parameters, which it correctly does, resulting in g: 'a -> unit when 'a : comparison
.
However, if I need to specify generic parameters explicitly, as in function h
, the compiler fails to infer constraints for some reason, resulting in a red squiggly under x
, saying "A type parameter is missing a constraint 'when 'b : comparison'."
This is especially annoying with functions that don't have non-generic parameters, i.e. let f<'a> = ()
, or whose non-generic parameters don't mention generic types, i.e. let f<'a> () = ()
, because I end up having to specify the constraints every single time I use such functions in another generic function:
let f<'a when 'a:comparison> () = ()
let g<'a> () = f<'a> () // Error: A type parameter is missing a constraint
let h () = f<'a> () // h gets inferred as unit -> unit, losing generic parameter
let k<'a when 'a:comparison> () = f<'a> () // The only working way
Is there a trick to have the compiler infer the constraints for me?