2017-09-14 40 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