How to pass optional labelled argumet from one function to another?


#1

Consider the following code with external FFI which works fine:

type operatorFunc

external _debounceTime : int -> ?hasSchedular:bool -> unit -> operatorFunc = "debounceTime"
  [@@bs.module "myModule"]

let debounceTime1 =  _debounceTime 1000 ()

However, if try to create a wrapper function (with an optional argument) around _debounceTime, then it doesn’t work:

let debounceTime ?hasSch () =  _debounceTime 1000 ~hasSchedular:hasSch ()
(* ERROR: This expression has type 'a option but an expression was expected of type bool *)

Of course, trying to use pattern matching on hasSch yields a warning which is right:

let debounceTime ?hasSch =
  match hasSch with
  | None -> _debounceTime 1000 ()
  | Some a -> _debounceTime 1000 ~hasSchedular:a ()

(* Warning 16: this optional argument cannot be erased. *)

The question here is - what is the idiomatic way to pass one optional function argument to another external function?


#2

See https://reasonml.github.io/docs/en/function#explicitly-passed-optional


#3

Ohh. Very stupid mistake. I was using ~ instead of ? for passing the optional argument. Thank you @yawaramin for pointing out.