gpt4 book ai didi

f# - 使用元组对抽象类型进行类型推断

转载 作者:行者123 更新时间:2023-12-02 09:31:55 25 4
gpt4 key购买 nike

基于this kvb 的答案,此代码编译(F#4)并运行:

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) }

但是如果最后一行被初始答案替换:

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

然后编译器提示错误 FS0768 :成员“Apply”不接受正确数量的参数,需要 2 个参数

我不明白为什么编译器似乎无法推断出 x 确实是一个元组。或者我还缺少其他问题吗?谢谢。

最佳答案

原因是,当您开始使用接口(interface)时,您就会进入 F# 对面向对象编程的支持,并且在 F# 中,所有OOP 互操作方法默认情况下都是元组

因此,Apply 方法被解释为采用两个方法参数的方法,而不是采用单个元组作为输入的函数。

关于f# - 使用元组对抽象类型进行类型推断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32177835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com