unknown6656
unknown6656

Reputation: 2963

F# Generic constraint not working due to "ambiguity"

I have an application, which renders piano sheets and I therefore have to abstract some musical notation concepts into record structures. To save some typing, I sometimes add the member FromTuple to some record types.
I also introduced the operator !>, which accepts a tuple and returns the appropriate tuple. I do however have the following issue:

FS0332: Could not resolve the ambiguity inherent in the use of the operator 'FromTuple' at or near this program point. Consider using type annotations to resolve the ambiguity.

I cannot locate the actual source of my error (I first thought that some record field names might be defined/used in multiple record types, but that does not seem to be the case).


The type definitions:

type xyz =
    {
        // some record fields
        Field1 : int
        Field2 : bool
        Field3 : string * string
    }
    with
        static member FromTuple (a, b, c) = { Field1 = a; Field2 = b; Field3 = c }

// more types defined like `xyz`

[<AutoOpen>]
module Globals =
    let inline (!>) x = (^b : (static member FromTuple : ^a -> ^b) x)

The faulty line (in a separate file):

//ERROR
let my_xyz : xyz = !> (315, false, ("foo", "bar"))

Upvotes: 4

Views: 142

Answers (1)

ildjarn
ildjarn

Reputation: 62975

Your xyz.FromTuple method takes three separate parameters: a: int, b: bool, and c: string * string; instead, you need it to take a single int * bool * (string * string). Do this by wrapping the parameters in another set of parenthesis:

static member FromTuple ((a, b, c)) = { Field1 = a; Field2 = b; Field3 = c }

Upvotes: 5

Related Questions