OCaml对象的多态性输入问题

Edg*_*ian 2 polymorphism ocaml typing object

在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
Run Code Online (Sandbox Code Playgroud)

并且最后一行method action无法输入错误消息:

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
Run Code Online (Sandbox Code Playgroud)

我也试过类似的东西:

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

但那也没有打字.

gar*_*jej 5

我想你想写下面的代码:

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
Run Code Online (Sandbox Code Playgroud)

我不确定你为什么在这里使用递归模块.他们不适合上课.特别是,接口不会传播到模块内的类.这就是为什么你需要action在体内明确写出多态方法的类型Third.