首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >封装融合的getter-setter函数

封装融合的getter-setter函数
EN

Stack Overflow用户
提问于 2016-07-29 20:18:30
回答 1查看 191关注 0票数 0

我很难用jooc包装D3-力的一个子集。库不使用对象属性,而是实现融合的getter-setter函数。

代码语言:javascript
复制
simulation.force("x", d3.forceX())  // setter
simulation.force("x")               // getter

我想找到一种方法来模拟OCaml中的相同类型的多态性。这是我目前的情况

代码语言:javascript
复制
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

我想要的是

代码语言:javascript
复制
let s = Force.simulation nodes in begin
  s##force "x" (Force.x ())
  s##force "x"  (* wishful thinking *)
 end
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-30 11:47:16

代码语言:javascript
复制
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
  • JavaScript字符串与ocaml字符串不兼容。使用Js.js_string Js.t
  • forceforce_set都将绑定到force。查看ocaml/2.8.1/手册/图书馆“方法名和下划线”
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38666861

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档