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: Adrien <camaradetux@g...>
Subject: Re: [Caml-list] Optional arguments "between" non-optional ones
On 23/09/2010, bluestorm <bluestorm.dylc@gmail.com> wrote:
> (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.

I actually expected types (?a:'a -> unit -> unit -> ...) and (unit ->
?a:'a -> unit -> ...) to work alike. I'm wondering why the arguments
are "dropped" from the first case but not the second.

Also, that's what I've done: I've simply swapped two parameters in a
function and it's working well now.

> 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.

Well spotted. I had gone a bit through the documentation but hadn't seen that.

> A fix is to add an annotation when defining h :
>
> let h (f : _ -> ?a:_ -> _) =
>  f () ()
>
> # let () = h f;;
> bouh!
> bouh!
>

I had tried to add an annotation too but (unit -> unit -> unit). Now,
thinking again about it and with your explanation, I understand better
why it had failed. I was trying to coerce f into a more restrictive
(or simply different?) type but the solution was keep the type of h's
argument broader.
Thanks.

--

Adrien Nader