2017-09-14 41 views
4
创建类

我知道OCaml中,我们可以创建一个类执行以下操作:OOP - 怎样才能在ReasonML

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

不过,我一直挣扎在如何做到这一点在寻找文档原因。谢谢。

回答

5

类和对象没有很好的记录,因为与更习惯的方法相比,这些功能为(通常)很少的好处增加了很多复杂性。但是,如果您知道OCaml语法的某些内容,您可以随时通过在线“尝试原因”操场将其转换为等同的原因。见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]; 
}; 
+1

类有它们的用例,他们只是很少见。 – RichouHunter