-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bindings with requires #66
Comments
First, even with the manual encoding, you could use gen_js_api to produce a binding for: type a_module = Ojs.t
val foo_internal: a_module -> string -> int and then simply define the user-visible let a = lazy (...)
let foo x = foo_internal (Lazy.force a) x Now, assuming that a Javascript global variable val foo: string -> int [@@js.global "A_module.foo"] One could also design some built-in gen_js_api support for this scheme, perhaps bound to OCaml modules: module A : sig
val foo: string -> int
end [@@js.require] within a module marked with the |
Thanks for your answer ! module A_internal : sig
type a_internal = Ojs.t
val foo : a_internal ->int -> int
end
let mod_a = lazy (Helpers.require "a")
let foo i = A_internal.foo mod_a i Implementing such an attribute could be a great idea to avoid code duplication or errors. Would you see this in gen_js_api ? I could give a try to add this attribute. |
Yes, absolutely! If some "small" additions to the tool simplify supporting common idioms in bindings, they are very much welcome. |
I picked up the branch in #68 and updated it with latest var internal_require=require;
function require$0(name){return internal_require(name.toString())} This is a no-go for most common JavaScript bundlers like Webpack, that need the string passed to If I compile using the Aside: I can create a PR with the updated branch if it helps. |
Writing quite some Node bindings - and it's annoying to bind modules to some globals and then reference those in |
Closing due to inactivity. |
Considering a javascript library containing a module A with only one static
function called foo taking a string and returning an int. This library
has to be required before usage, but I did not found anything about require in
the documentation.
The first implementation I made was:
To call the function foo in ocaml code, I have to write:
But I'd like to get rid of the require line. This require would be done lazily into the
module, allowing the user to write directly
let i = A.foo "bar"
to call thefunction.
My first thought was to do something like:
But it becomes very annoying to write this when the javascript library has many
methods. What would be the best way to do this kind of things ? Does it match
the philosophy of javascript bindings ?
The text was updated successfully, but these errors were encountered: