Skip to content

Addition operator required by type parameter constraint can't be invoked #12385

Description

@brianberns

Repro steps

Consider the following code:

let inline add<'T when '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 inline add<'T when '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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions