Calling a generic method during object construction incorrectly constrains the generic to a type
When calling a generic method directly during object construction I get a warning that my code is causing my method to be less generic than the type annotation.
Repro steps
type Test() as self =
do
self.PrintString 12
member this.PrintString<'a> (x : 'a) = Console.WriteLine(x.ToString())
Expected behavior
The above compiles without warnings.
Actual behavior
I get the warning FS0064: This construct causes code to be less generic than indicated by the type annotations. The type variable 'a has been constrained to be type 'int'
Related information
.NET 9.0
I think this is kinda known, not sure what needs to be done in order to fix it without breaking compatibility, but funny enough, this will fix it:
type Test() as self =
do
self.PrintString 12
self.PrintString 12.0
member _.PrintString<'T> (x : 'T) : unit = System.Console.WriteLine(x.ToString())
Even smaller repro would be Doesn't work
type Test() as self =
do
self.PrintString 12
member _.PrintString<'T> (x : 'T) = ()
Does work
type Test() as self =
do
self.PrintString 12
member _.PrintString<'T> (x : 'T) : unit = ()