2011-01-07 49 views
3

我有一个简单的问题。为什么这不起作用?使用F的类型成员中的元组#

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 

的错误是:

类型约束不匹配。 float类型*浮子*浮子不与键入Test1类型 '浮子*浮子*浮动' 兼容不与类型 '测试1'

类型“浮子*浮子兼容* float'与类型'Test1'不兼容

回答

5

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

type Test2() = 
    member o.test ((x: float, y: float, z: float)) = printfn "test" 
    member o.test (x: Test1) = o.test x.toTuple 

唐赛姆here的见的解释。

请注意,如果你不想增加额外的括号,你仍然可以解构的元组,并使用多个参数调用:

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) 
+0

感谢解释 – 2011-01-07 13:46:08

4

将您的第一个方法重命名为Type2而不是test。你的第二种方法是遮蔽你的第一个方法,从而混淆了编译器。

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

type Test2() = 
    member o.print (x: float, y: float, z: float) = printfn "test" 
    member o.test (x: Test1) = o.print x.toTuple