Skip to content
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 to JavaScript functions that expect value undefined #97

Open
jchavarri opened this issue Sep 21, 2019 · 0 comments
Open

Bindings to JavaScript functions that expect value undefined #97

jchavarri opened this issue Sep 21, 2019 · 0 comments

Comments

@jchavarri
Copy link
Contributor

jchavarri commented Sep 21, 2019

There are JavaScript functions that expect the value undefined to be passed to them, not null. gen_js_api doesn't provide (as far as I know) a way to do this, as option types get converted to null.

I added this to the bindings I am working on:

[@@@js.stop]
type 'a option_undefined = 'a option
[@@@js.start]
[@@@js.implem
type 'a option_undefined = 'a option
external equals: Ojs.t -> Ojs.t -> bool = "caml_js_equals"
external pure_js_expr: string -> Ojs.t = "caml_pure_js_expr"
let undefined = pure_js_expr "undefined"
let option_undefined_of_js f x =
  if equals x undefined then None
  else Some (f x)

let option_undefined_to_js f = function
  | Some x -> f x
  | None -> undefined
]

But I wonder if this type option_undefined could be something built in as part of gen_js_api? Or maybe, as an annotation to type option, instead of a full new built-in type.

@jchavarri jchavarri changed the title Bindings to JavaScript functions that expect undefined values Bindings to JavaScript functions that expect value undefined Sep 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant