F # General restriction not working due to "ambiguity"

I have an application that displays piano sheets, and therefore I have to abstract some musical notations in the recording. To preserve some typing, I sometimes add a member FromTupleto some record types.
I also introduced an operator !>that takes a tuple and returns the corresponding tuple. However, I have the following problem:

FS0332: The ambiguity inherent in using the FromTuple statement at or near this program point could not be resolved. Consider using type annotations to eliminate ambiguity.

I cannot find the actual source of my error (at first I thought that some record field names could be defined / used in several record types, but this does not seem to be the case).


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)

Invalid line (in a separate file):

//ERROR
let my_xyz : xyz = !> (315, false, ("foo", "bar"))
+4
source share
1 answer

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

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

Source: https://habr.com/ru/post/1678981/


All Articles