2016-07-29 115 views
0

我在使用jooc包裝d3-force的子集時遇到了問題。該庫不使用對象屬性,而是實現融合的getter-setter函數,例如,包裝熔融吸氣劑功能

simulation.force("x", d3.forceX()) // setter 
simulation.force("x")    // getter 

我想找到一種方法來模擬OCaml中的同一種多態性。這是我目前有

module Force = struct 
    class type force = object 
    (* not important *) 
    end 

    let x(): force Js.t = Js.Unsafe.meth_call __d3 "forceX" [||] 

    class type simulation = object 
    method force : string -> #force Js.t -> unit Js.meth 
    end 

    let simulation nodes: simulation Js.t = 
    Js.Unsafe.(meth_call __d3 "forceSimulation" [|inject nodes|]) 
end 

而這裏的什麼我後

let s = Force.simulation nodes in begin 
    s##force "x" (Force.x()) 
    s##force "x" (* wishful thinking *) 
end 

回答

1
class type simulation = object 
    method force_set : Js.js_string Js.t -> #force Js.t -> unit Js.meth 
    method force : Js.js_string Js.t -> #force Js.t Js.meth 
end 
+0

非常感謝,非常感謝! –