fsharp icon indicating copy to clipboard operation
fsharp copied to clipboard

Calling a generic method during object construction incorrectly constrains the generic to a type

Open steveisaac opened this issue 4 months ago • 2 comments

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

steveisaac avatar Oct 06 '25 14:10 steveisaac

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())

vzarytovskii avatar Oct 07 '25 08:10 vzarytovskii

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 = ()

vzarytovskii avatar Oct 07 '25 09:10 vzarytovskii