使用F#的类型成员中的元组

Old*_*vec 3 f# types tuples member

我有一个简单的问题.为什么这不起作用?

type Test1() =
  member o.toTuple = 1.,2.,3.

type Test2() =
  member o.test (x: float, y: float, z: float) = printfn "test"
  member o.test (x: Test1) = o.test x.toTuple
Run Code Online (Sandbox Code Playgroud)

错误是:

类型约束不匹配.float*float*float类型与类型Test1不兼容类型'float*float*float'与类型'Test1'不兼容

'float*float*float'类型与'Test1'类型不兼容

Str*_*ger 5

这不起作用,因为在重载的情况下,第一个成员测试被视为多参数方法.如果你需要一个tupled,你必须添加额外的括号:

type Test2() =
    member o.test ((x: float, y: float, z: float)) = printfn "test"
    member o.test (x: Test1) = o.test x.toTuple
Run Code Online (Sandbox Code Playgroud)

在此处查看Don Syme的说明.

请注意,如果您不想添加额外的parens,您仍然可以解构元组并使用多参数调用:

type Test2() =
    member o.test (x: float, y: float, z: float) = printfn "test"
    member o.test (x: Test1) = let a,b,c = x.toTuple in o.test(a,b,c)
Run Code Online (Sandbox Code Playgroud)