You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
let inlineadd<'Twhen'T:(static member(+):'T*'T->'T)>(a :'T)(b :'T)=
a + b
Expected behavior
Code should compile and successfully add the two arguments.
Actual behavior
Compiler error: A type parameter is missing a constraint 'when ( ^T or ^?11088896) : (static member (+) : ^T * ^?11088896 -> ^?11088897)'
Known workarounds
The following variation works:
let inlineadd<'Twhen'T:(static member(+):'T*'T->'T)>(a :'T)(b :'T)=(^T:(static member(+):^T * ^T -> ^T)(a,b))
However, it also results in a compiler warning: Member constraints with the name 'op_Addition' are given special status by the F# compiler as certain .NET types are implicitly augmented with this member. This may result in runtime failures if you attempt to invoke the member constraint from your own code.
The text was updated successfully, but these errors were encountered:
This issue is tracked by #9633. The general problem is that half way through inference a constraint arises that is falsely unified with the explicit consttraint. It's actually fixed in #6805 but that is a long way from going in and it's hard to isolate out the fix and extensive testing.
FWIW you can also omit the explicit type parameter declarations:
let inlineaddSame(x:'T)(y:'T):'T = x + y
let inlineadd<'Twhen'T:(static member(+):'T*'T->'T)>(a :'T)(b :'T):'T =
addSame a b
This actually gives a fairly general technique to avoid this problem -
first define a helper function without explicit type parameter declarations, reducing the genericity
then call that from your code with explicit type parameter declarations
Repro steps
Consider the following code:
Expected behavior
Code should compile and successfully add the two arguments.
Actual behavior
Compiler error:
A type parameter is missing a constraint 'when ( ^T or ^?11088896) : (static member (+) : ^T * ^?11088896 -> ^?11088897)'
Known workarounds
The following variation works:
However, it also results in a compiler warning:
Member constraints with the name 'op_Addition' are given special status by the F# compiler as certain .NET types are implicitly augmented with this member. This may result in runtime failures if you attempt to invoke the member constraint from your own code.
The text was updated successfully, but these errors were encountered: