在OCaml中使用类

在OCaml中使用类,ocaml,Ocaml,我想在类t中用我的类型test\u type创建函数。我的代码: type test_type = [`t1|`t2] let get_types = function | `t1 -> "t1" | `t2 -> "t2";; class type class_types = object method t_types : test_type method test : (string -> string -> test_type

我想在类
t
中用我的类型
test\u type
创建函数。我的代码:

type test_type = [`t1|`t2]

let get_types =
  function
   | `t1 -> "t1"
   | `t2 -> "t2";;

class type class_types =
  object
    method t_types : test_type
    method test : (string -> string -> test_type -> unit) -> unit
end;;

class t : class_types =
  object
    method test par1 ?(par2="no par2") ?(par3=`t1) () =
      print_endline("--->"^par1);
      print_endline("--->"^par2);
      print_endline("--->"^get_types par3)
end;;

let t_run = new t;;
t_run # test "parametr1" ~par3:`t2 ();;
是返回错误吗

The class type is not matched by the class type class_types
The first class type has no method t_types

如何做到这一点?

简单地说,您的
t
实现缺少方法
t\u-types
,该方法在class-type
class\u-types
中定义


除此之外,方法
test
的类型是
string->par2:string->par3:test\u type->unit->unit
,这与类类型中的方法不兼容。

仅仅是
t
的实现缺少方法
t\u-types
,该方法是在类类型
class\u-types
中定义的


除此之外,方法
test
的类型是
string->par2:string->par3:test\u type->unit->unit
,这与类类型中的方法不兼容。

仅仅是
t
的实现缺少方法
t\u-types
,该方法是在类类型
class\u-types
中定义的


除此之外,方法
test
的类型是
string->par2:string->par3:test\u type->unit->unit
,这与类类型中的方法不兼容。

仅仅是
t
的实现缺少方法
t\u-types
,该方法是在类类型
class\u-types
中定义的


除此之外,方法
test
的类型是
string->par2:string->par3:test\u type->unit->unit
,这与类类型的方法不兼容。

非常感谢,你帮了我很多。非常感谢,你帮了我很多。非常感谢,你帮了我很多。非常感谢,你帮了我很多。