9

我知道在 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;;

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

4

1 回答 1

11

类和对象的文档记录不是很好,因为与更惯用的方法相比,这些特性增加了很多复杂性(通常)带来的好处很少。但是,如果您知道某事的 OCaml 语法,您总是可以通过在线“Try Reason”游乐场转换它来查看 Reason 等价物是什么。在此处查看您的示例,它为我们提供了以下信息:

class stack_of_ints = {
  as self;
  val mutable the_list: list int = []; /* instance variable */
  pub push x =>
    /* push method */
    the_list = [x, ...the_list];
};
于 2017-09-14T13:30:42.087 回答