OOP - 如何在ReasonML中创建一个类

Cha*_*man 9 oop ocaml reason

我知道在OCaml中,可以创建一个类来执行以下操作:

class stack_of_ints =
  object (self)
    val mutable the_list = ( [] : int list ) (* instance variable *)
    method push x =                        (* push method *)
      the_list <- x :: the_list
  end;;
Run Code Online (Sandbox Code Playgroud)

但是,我一直在努力寻找有关如何在Reason中执行此操作的文档.谢谢.

gle*_*nsl 12

类和对象没有很好地记录,因为与更惯用的方法相比,这些特性为(通常)非常少的好处增加了很多复杂性.但是如果你知道某些东西的OCaml语法,你总是可以通过在线"尝试原因"游乐场转换它来看看它的等效原因.在这里查看您的示例,它给出了我们:

class stack_of_ints = {
  as self;
  val mutable the_list: list int = []; /* instance variable */
  pub push x =>
    /* push method */
    the_list = [x, ...the_list];
};
Run Code Online (Sandbox Code Playgroud)

  • 类有其用例,它们很少见。 (2认同)