gen_js_api icon indicating copy to clipboard operation
gen_js_api copied to clipboard

Bindings to JavaScript functions that expect value undefined

Open jchavarri opened this issue 6 years ago • 0 comments

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 avatar Sep 21 '19 14:09 jchavarri