type inference on abstract type with a tuple

限于喜欢 提交于 2019-12-11 03:42:50

问题


Based on this kvb's answer, this code compiles (F#4) and runs :

type Untupler = abstract Apply : 'u * 'u -> 'u

let myotherFun arg1 arg2 =
    printfn "myotherFun result is : %A %A" arg1 arg2

let myFunction tup1 tup2 (i:Untupler) =
    myotherFun (i.Apply tup1) (i.Apply tup2)

let reskvb = myFunction (1,2) ("Hello","World") { new Untupler with member __.Apply (x,y) = snd (x,y) }

But if the last line is replaced by the initial answer :

let reskvb = myFunction (1,2) ("Hello","World") { new Untupler with member __.Apply x = fst x }

then the compiler complains with error FS0768 : The member 'Apply' does not accept the correct number of arguments, 2 arguments are expected

I do not understand why the compiler seems to fail to infer that x is indeed a tuple. Or is there another issue I am missing ? Thx.


回答1:


The reason for this is that when you start using interfaces, you move into F#'s support for Object-Oriented Programming, and in F#, all OOP interop methods are tupled by default.

Thus, the Apply method is interpreted as being a method that takes two method arguments, rather than a function that takes a single tuple as input.



来源:https://stackoverflow.com/questions/32177835/type-inference-on-abstract-type-with-a-tuple

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!