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

标签 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;;

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

最佳答案

没有很好地记录类和对象,因为与惯用的方法相比,这些功能增加了很多复杂性,(通常)带来的好处很少。但是,如果您知道OCaml语法的含义,则可以通过在线“Try Reason”游乐场对其进行转换,从而始终看到等效的原因。参见your example here,它给我们提供了这一点:

class stack_of_ints = {
  as self;
  val mutable the_list: list int = []; /* instance variable */
  pub push x =>
    /* push method */
    the_list = [x, ...the_list];
};

关于OOP-如何在ReasonML中创建一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46219422/

相关文章:

ocaml - 哪些是适用于处理多维数组的高效、类型推断语言的示例

reason - 如何在 ReScript 中为任意记录类型实现哈希函数?

import - 在 ReasonML 中为导入的模块使用自定义名称

正确的面向对象技术的 c# 实践

oop - 如何使用 Playground/Workspace(而不是通过系统浏览器)创建方法?

c++ - 在通过构造函数传递变量的对象中创建对象

parsing - OCaml 解析函数

functional-programming - 如何在 Ocaml 中编写模式匹配以便易于扩展?

javascript - javascript 中的不可变类型

node.js - 将 ReasonML 函数部署到 Google Cloud Functions