Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Optional arguments "between" non-optional ones
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: bluestorm <bluestorm.dylc@g...>
Subject: Re: [Caml-list] Optional arguments "between" non-optional ones
(sorry for any double-posting)

The problem is that in your declaration of h, the inferred type for f
is of the form (unit -> unit -> ...), and you use it with the
different type (unit -> ?a:'a -> unit -> ...).

Changing ?a to be the first parameter of f change f's type to (?a:'a
-> unit -> unit -> ...). OCaml knows that it can implicitly coerce
functions when the optional parameters appear in the first position.

This is explained in the manual :
http://caml.inria.fr/pub/docs/manual-ocaml/manual006.html#htoc39

> However, in the specific case where the expected type
> is a non-labeled function type, and the argument is
> a function expecting optional parameters, the compiler
> will attempt to transform the argument to have it match
> the expected type, by passing None for all optional parameters.

A fix is to add an annotation when defining h :

let h (f : _ -> ?a:_ -> _) =
 f () ()

# let () = h f;;
bouh!
bouh!