Skip to content

Bindings to JavaScript functions that expect value undefined #97

@jchavarri

Description

@jchavarri

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions