English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] Optional arguments and type annotations
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2001-11-01 (05:46)
From: Jacques Garrigue <garrigue@k...>
Subject: Re: [Caml-list] Optional arguments and type annotations
From: Christian RINDERKNECHT <rinderkn@hugo.int-evry.fr>

> I am used to annotate the top-level functions with their types, and I
> was surprised to see that I must write the following to make it
> type-check: 
> #let bump ?(step:int option) (x:int) =
>    match step with
>      None -> x * 2
>    | Some y -> x + y
>  ;;
> val bump : ?step:int -> int -> int = <fun>
> and then there is a discrepancy between the "int option" annotation in
> the .ml and the "int" in the interface.

As you can see with the pattern matching, from inside the function
step is of type "int option". But since it is optional, when applying
the function you can simply pass an argument of type "int", which is
reflected by the external type of the function.

This is not to say that there is no discrepancy: actually, if you pass
an argument with label ?step: instead of ~step:, it has to be of type
"int option", so that we could have written "?step:int option -> int
-> int". But since an optional argument must always have type "t
option", the "option" part is redondant, and omitted in the
printed and parsed types (it actually appears inside the type checker).

Hope this is not too misleading.

Jacques Garrigue
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr