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
Labeled arguments and polymorphic return types
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-08-13 (08:52)
From: Jacques Garrigue <garrigue@m...>
Subject: Re: [Caml-list] Labeled arguments and polymorphic return types
From: Andre Nathan <andre@digirati.com.br>

> Consider the following definition:
> # let f ~a = print_int a; raise Exit;;
> val f : a:int -> 'a = <fun>
> If I call f without using the argument label, I get the following
> warning:
> # f 1;;
> Warning X: this argument will not be used by the function.
> - : a:int -> 'a = <fun>
> The call works if I use the label ("f ~a:1"), if I define f without a
> label for its argument, or if I remove the "raise" statement (causing
> the return value to be unit instead of 'a).
> Why is this warning generated?

Reading your description, I think you are misunderstanding the
semantics of labels. Namely, you are only allowed to omit label names
if all the arguments are known to be passed, which in particular
excludes all functions whose return type is 'a. In that case, a
non-labelled argument is assumed to correspond to a (yet unknown)
non-labelled formal parameter. 

The above case is tricky, but here is a simple one:
# let f ~a x = a :: x;;
val f : a:'a -> 'a list -> 'a list = <fun>
# f 1 [];;
- : int list = [1]
# f [];; 
- : a:'a -> 'a list = <fun>
In the last line, [] does not replace ~a but x.

Now, this explains the above warning: since there is no unlabelled
parameter, but the return type is polymorphic, your unlabelled
argument will be simply discarded, which is probably not what you
wanted. The compiler is kind enough to inform you.

Jacques Garrigue