ocamljs-of-ocaml

binding library : returning untyped object - method definition


Part of a library I'm trying to bind returns an object -

Editor.prototype.getHandlers = function() {
  return {
    'shape.append': require('./draw/AppendShapeHandler'),
    'shape.create': require('./draw/CreateShapeHandler')
  };
};

What I can't figure out is how to specify the class type as the returned object is anonymous :

class type editor = object
  method getHandlers : ? Js.t Js.opt Js.meth
end

Could anyone suggest a way forward here?

Thanks

Nick


Solution

  • For this case, perhaps something like:

    class type editor = object
      method getHandlers : <shape_append : Js.js_string Js.t Js.meth> Js.t Js.meth
    end
    

    More example:

    class type server = object
      method listen : int -> (unit -> unit) Js.callback -> unit Js.meth
      method close : (unit -> unit) Js.callback -> unit Js.meth
      method address :
                <address: Js.js_string Js.t Js.readonly_prop;
                 family : Js.js_string Js.t Js.readonly_prop;
                 port: Js.js_string Js.t Js.readonly_prop> Js.t Js.meth
    end
    

    This approach, of binding this way, works but as I learned in my OCaml nodejs bindings, its better to write at a higher level rather than do these bindings. https://github.com/fxfactorial/ocaml-nodejs (Look at early git history for many more examples like this)