Inheritance 关闭ocaml中的对象类型 我的QT5绑定有一个OcAML类的继承树,它把C++对象作为参数。因此,它沿着继承链工作。基类有一个方法obj返回一个'at',其中'a是自类型。现在的问题是,用户需要能够继承我的ocaml类,因此我必须关闭类型,或者删除或隐藏方法obj和obj参数。用户无法创建派生的C++对象。 type 'a t (* type of the c++ objects *) (* ocaml wrapper around the c++ object *) class foo (obj : 'a t) = object(self:'a) method obj = obj method foo = "foo" end (* external function returning c++ object *) let make_foo_t () = ((Obj.magic 0) : foo t) (* way for the user to create a foo *) let make () = new foo (make_foo_t ()) (* class the user can derive from hiding the c++ object *) class bar = object inherit foo (make_foo_t ()) method bar = "bar" end (* user *) let x = new bar;; class derived_bar = object inherit bar method dervived_bar = "derived_bar" end

Inheritance 关闭ocaml中的对象类型 我的QT5绑定有一个OcAML类的继承树,它把C++对象作为参数。因此,它沿着继承链工作。基类有一个方法obj返回一个'at',其中'a是自类型。现在的问题是,用户需要能够继承我的ocaml类,因此我必须关闭类型,或者删除或隐藏方法obj和obj参数。用户无法创建派生的C++对象。 type 'a t (* type of the c++ objects *) (* ocaml wrapper around the c++ object *) class foo (obj : 'a t) = object(self:'a) method obj = obj method foo = "foo" end (* external function returning c++ object *) let make_foo_t () = ((Obj.magic 0) : foo t) (* way for the user to create a foo *) let make () = new foo (make_foo_t ()) (* class the user can derive from hiding the c++ object *) class bar = object inherit foo (make_foo_t ()) method bar = "bar" end (* user *) let x = new bar;; class derived_bar = object inherit bar method dervived_bar = "derived_bar" end,inheritance,ocaml,self-type,Inheritance,Ocaml,Self Type,问题是我不能让班级酒吧工作。无论我做什么,ocaml都会抱怨“Self-type不能与封闭对象类型统一”或“Self-type不能逃逸它的类” 想法?经过多次尝试,从零开始,我找到了一个即使不美观也能奏效的解决方案。仍在寻找更好的产品: module type Make = sig val stub : unit -> #foo t end class bar () = let obj = let module E = (val (module struct

问题是我不能让班级酒吧工作。无论我做什么,ocaml都会抱怨“Self-type不能与封闭对象类型统一”或“Self-type不能逃逸它的类”


想法?

经过多次尝试,从零开始,我找到了一个即使不美观也能奏效的解决方案。仍在寻找更好的产品:

module type Make = sig
  val stub : unit -> #foo t
end

class bar () =
  let obj =
    let module E = (val (module struct
      external stub : unit -> #foo t = "stub"
    end) : Make)
    in
    E.stub text
  in
object
  inherit foo obj
end
我将“stub”声明为外部函数,返回'atwhere'作为foo的直接或间接子类。我使用一个本地第一类模块来限制存根在类栏中的暴露,这样即使没有.mli文件也不会被滥用


注意:类栏必须采用参数(此处为单位),否则不会键入。我花了一段时间才明白这一点。

经过多次尝试,从零开始,我找到了一个即使不美观也能奏效的解决方案。仍在寻找更好的产品:

module type Make = sig
  val stub : unit -> #foo t
end

class bar () =
  let obj =
    let module E = (val (module struct
      external stub : unit -> #foo t = "stub"
    end) : Make)
    in
    E.stub text
  in
object
  inherit foo obj
end
我将“stub”声明为外部函数,返回'atwhere'作为foo的直接或间接子类。我使用一个本地第一类模块来限制存根在类栏中的暴露,这样即使没有.mli文件也不会被滥用


注意:类栏必须采用参数(此处为单位),否则不会键入。我花了一段时间才弄明白。

为什么酒吧需要一个虚拟arg?为什么酒吧需要一个虚拟arg?