gpt4 book ai didi

object - OCaml 对象的多态类型问题

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

在 4.03.0 上。

我有这段代码,基本上:

module rec First : sig

type field
type 'a target = <at_least_this_method: field; .. > as 'a

end = First
and Second : sig

class foo : object
method at_least_this_method : First.field
end

end = Second
and Third : sig

class usage :
object

method action : #Second.foo First.target -> unit

end

end = struct

class usage = object
method action (a : #Second.foo First.target) = ()
end

end

方法操作的最后一行无法键入并显示错误消息:

Error: Some type variables are unbound in this type:
class usage :
object method action : #Second.foo First.target -> unit end
The method action has type (#Second.foo as 'a) First.target -> unit
where 'a is unbound

我也尝试过类似的方法:

  class usage = object
method action = fun (type t) (b : (#Second.foo as t) First.target) -> ()
end

但这也没有输入。

最佳答案

我想您想编写以下代码:

module rec First : sig
type field
class type target = object method at_least_this_method: field end
end = First
and Third : sig
class usage :
object
method action : #First.target -> unit
end
end = struct
class usage = object
method action : 'a. (#First.target as 'a) -> unit = fun a -> ()
end
end

我不确定你为什么在这里使用递归模块。它们不能很好地适应类。特别是,接口(interface)不会传播到模块内的类。这就是为什么您需要在 Third 主体中显式编写多态方法 action 的类型。

关于object - OCaml 对象的多态类型问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37910498/

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