Issue with labeled arguments in OCaml -
this line of ocaml:
let c ~f = f() in let () = c (fun () -> ()) in ()
leads following error on c (fun () -> ())
expression:
file "tst.ml", line 1, characters 27-43: error: expression has type f:(unit -> (unit -> unit) -> 'a) -> 'a expression expected of type unit
without ~
, there no error. there no error if explicitly label argument c ~f:(fun -> ())
.
how labeling messing things up?
here's happening, believe. you're trying use following special case functions labeled parameters:
as special case, if function has known arity, arguments unlabeled, , number matches number of non-optional parameters, labels ignored , non-optional parameters matched in definition order. optional arguments defaulted.
(section 6.7.1 of ocaml manual.)
but in definition, c
doesn't have known arity. because f
can return function, , hence c
can return function.
since special case doesn't apply, compiler has assume labeled argument ~f:
hasn't appeared yet, , does return function. hence typing see in example.
if redefine c
has known arity (so can't return function), things work expect.
# let c ~f = [ f () ];; val c : f:(unit -> 'a) -> 'a list = <fun> # c (fun () -> ());; - : unit list = [()]
Comments
Post a Comment